Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with Java Server Pages(JSP)

In this article we will describe about the Web development in java with Java Server Pages(jsp)

JSP is a server side technology of programming which is used in order to create the dynamic web pages. Like the servlets, JSP also have an access to different APIs of Java.In a java server page, the java code is inserted inside HTML content. JSP is an extended form of servlet. The page of JSP consists of the HTML tags, JSP tags and scripting elements. The JSP page is able to return the type of content including static and dynamic as the requirement in response to the request.

The static content can be in the form of HTML, XML and text. The dynamic content can be in the form of custom tags, java beans and code of java.There is a container in the web server that is required in order to process the pages of JSP. The container of JSP basically works with the web server in order to provide the related services which this type of page requires.

Description:

The input from users can be collected through the forms of webpage by using JSP and the records from database system are able to appear in the form of view respectively. In order to retrieve the information containing data from the database system, the available tags of JSP are used and these tags are used for different related functionalities like going from one page to another and similarly for other means as well.The phases of JSP life cycle include the translation, compilation and execution. The web browser sends HTTP request for a normal webpage to the web server but in JSP the web server first checks that if the particular request is made for a JSP page then it forwards this request to the container.

The container of JSP loads the page of JSP and it is converted to the content of servlet and the conversion is done in a simple manner in which the text from this respective JSP page gets converted to the print statements for servlets. The container of JSP then compiles this servlet and the request is sent to the container of servlet and it loads the class of servlet in it and the execution process gets started respectively. The output comes in the form of HTML and this is produced by the servlet which is executed by the container of servlet and this output in the form of HTML is passed to the web server which is available inside the HTTP response. The web server sends this HTTP response to the web browser of user in the form of static content of HTML and in this way the web browser handles this dynamic webpage in the HTTP response like a static webpage.

The activity of initialization of JSP is performed by overriding the jspInit method available in the interface of JSP and it basically initializes the resources and reads the data from a configuration file. The destruction activity related to cleaning of JSP is performed by overriding the jspDestroy method and it basically writes a configuration file and releases the consumed resources respectively. There is a method available known as jspService like the method of service in servlet that deals with the request and response for the client and server model. The directives in JSP are used in order to provide the information regarding the processing about the JSP to the container of JSP and the directive is present at the top of JSP file and the request made to this page do not put any effect on the validity of directive. The expression of JSP inserts an expression of java which gets evaluated at times when the processing in this page is done. The scriptlet of JSP inserts code in the document of JSP. The scriptlet is basically java code embedded in jsp page with the help of tags. In a JSP, the code of servlet can be marked with special tags which are available.

There are separate tags available in JSP and most of them start with <% and ends with %> .JSP allows the user to enter the dynamic elements inside the HTML and these web pages are built on the top of servlets API. The code is written in the form of HTML and the respective tags provided in JSP are used and this code can be easily modified whenever some changes are required and there is no need to write the println statements so this technology is better than servlets in this manner.Java Server Pages support all the related tags which are present in HTML. Some of the available tags in JSP includes <jsp:attribute>, <jsp:body>, <jsp:declaration>,<jsp:element>, <jsp:fallback>, <jsp:forward>, <jsp:include>, <jsp:scriptlet> and <jsp:setproperty>. The tags of JSP put an effect on the runtime behavior of the pages. The directives of JSP are represented using the form of <%@.....%>. The scriptlet which is known as scripting element is represented using the form of <%@.....%>. The expression of JSP is represented in this form <%@.....%>. In order to add the variables and define the methods <%@.....%> is used.

Importance:

The servlet gets invoked for each request and the full page of JSP can be translated once into a servlet in an easy manner. The pages of JSP are translated to servlets and the servlets runs when the request comes and client is not able to see anything related to JSP. In order to understand the working behind JSP, there is a need to first have a complete understanding about servlets. The main advantage of JSP is that the static and dynamic content gets separated i.e., the static content includes the design of a webpage and the dynamic content includes the business logic which helps in proper understanding and the page can be maintained in an easy manner.JSP is easy to program and it basically provides the facility in order to develop the dynamic web pages and uses the Java programming language and it consists of scripting and different tags.

The JSP_work of web application screenshots are shown as follows:

Date and Time

Figure 1: Date and Time

The Figure 1 shows the output from the running application and it prints the current date and time of the system at the start.

Work of form in page of JSP

Figure 2: Work of form in page of JSP

The Figure 2 contains more work and the user can insert values and these values can later be saved in the table of database system if required.

Get input values by user and save at the backend

Figure 3: Get input values by user and save at the backend

The Figure 3 shows the values inserted by the user and after saving in the application and click of a button, these are retrieved and shown in the web browser successfully.

Output from the Web Application of JSP

Figure 4: Output from the Web Application of JSP

The Figure 4 shows the output and the values inserted by the user from the web browser which appear at the backend and the success of web application is highlighted as displayed in the console.

Implementation & Code:

Listing 1: Index.jsp

<%@page language="java" %>
<%@page import="java.util.Date" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is a JSP Page</title>
</head>
<body>
<h1 style="background-color: burlywood;">This page displays the current date and time of the system</h1>
<p style="background-color: graytext;"></p>
<%! 
public String systemdt(){
                Date d=new Date();
                String current_time=d.toString();                        
returncurrent_time;
            }
        %>
<h2 style="background-color: darkgoldenrod;"><%=systemdt()%></h2>
<form>
        Customer ID: <input type="text" name="idc"><br>
        Customer Name: <input type="text" name="cusname"><br>
        Customer Gender: <input type="text" name="gc"><br>
        Customer Address: <input type="text" name="caddr"><br>
        Customer Profession: <input type="text" name="cprof"><br>
<input type="submit" value="Create Record of Customer">
</form>
<p style="background-color: yellowgreen;"The entered input by the user include the following></p>
<p style="background-color: highlight;"Customer ID =>
<%=                 
request.getParameter("idc")
         %>
</p>
<p style="background-color: highlight;"Customer Name =>
<%=                 
request.getParameter("cusname")
         %>
</p>
<p style="background-color: highlight;"Customer Gender =>
<%=                 
request.getParameter("gc")
         %>
</p>
<p style="background-color: highlight;"Customer Address =>
<%=                 
request.getParameter("caddr")
         %>
</p>
<p style="background-color: highlight;"Customer Profession =>
<%=                 
request.getParameter("cprof")
         %>
</p>
<% System.out.println("Work of this page has finished successfully");%>
</body>
</html>

Conclusion:

In this page of JSP, it contains the work as presented in above screenshots. The tags of JSP are used and java code inserted inside HTML and the values entered by the user in the browser are also retrieved successfully.

Hope you liked it. See you next time.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login