Sivaram Kappaganthu
Sivaram Kappaganthu

Reputation: 53

Purpose of closing html and body tags in Jsp page

I have very basic question on Jsp and HTML and the below example is from Java EE7 black book.

Login.html

<html> 
<body>
    <pre>
    <form action="LoginProcess.jsp">
        <b>User Name</b> : <input type="text" name="uname"/>
        <b>Password</b>  : <input type="password" name="pass"/>
        <input type="submit" value="LogIN"/>
    </form> 
    </pre>
</body>
</html>

Below is the code for JSp

<%@page import="java.sql.*" errorPage="\MyError.jsp"%>
<html>
<body>
    <% 
    Connection con=null;
    String uname=request.getParameter("uname");
    String pass=request.getParameter("pass");
    try 
    {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.123:1521:XE","scott","tiger");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("select * from userdetails where uname='"+uname+ "' and pass='"+ pass +"'");
        if (!rs.next())
        {   

    %>

    User details given for user name : <%=request.getParameter("uname")%> and 
    password : <%=request.getParameter("pass")%> are not valid <br/> Try again 
    <%@include file="Login.html"%>
</body>
</html>
<% 
        return; 
    }//if
}//try
finally 
{
    try
    {
        con.close();
    }catch(Exception e){}
}//finally
%>

This is a Home Page <br/>
Welcome, <%=uname%>

My question is regarding </body> & </html> tags that hang in the middle of the code right above return statement [dont know how to highlight them in code]. I did not understand the purpose of those two tags. I got confused on why the html and body tags are closed. And How can further code is rendered after html tag is closed. Could someone please throw some light on this.

Thanks in advance .

Upvotes: 1

Views: 2250

Answers (1)

Shailesh Saxena
Shailesh Saxena

Reputation: 3512

So, though its very old style code, Let me put some light on it (for sake of other beginners too):

First of all the code given by you is very ugly and old style.

As academic point of view-

In a jsp, you can have 3 types of basic tags-

  1. Scriptlets: <% %>
  2. Declarations: <%! %>
  3. Expression: <%= %>

All jsp pages are internally converted to equivalent servlets and a default method _jspService(httpServletRequest, httpServletResponse) is getting created for each jsp in equivalent servlet file.

The code placed inside scriptlets automatically goes inside that _jspService(httpServletRequest, httpServletResponse)

Hence:

  • Variables declared in scriptlets become local variables of _jspService(httpServletRequest, httpServletResponse) method.
  • Because java don't support nesting of methods, we can't place any method inside scriptlets.
  • In declaration tags you can declare variables.
  • Code placed inside declaration tags of a jsp comes outside of the _jspService(httpServletRequest, httpServletResponse) method of equivalent servlet
  • And variables declared inside declaration tags become instance variables of equivalent servlet class.
  • In jsp we have 9 types of implicit objects(request, response, out, page etc.)
  • And all implicit objects are local variables of _jspService(req, res) method, so these implicit objects are not accessible inside declaration tags of jsp.
  • Expression tags evaluates the given expression and writes the generated output to browser window as web page content.
  • Arithmetic operations, logical operations, java method calls with return values etc. can be placed inside expression tags.

Apart from these 3 scripting tags, we have 3 directive tags in jsp

  1. Page Directive tag: <%@page %> This tag provides global information to the jsp equivalent servlet program like importing packages, increasing buffer size, specifying error page etc.
  2. Include Directive Tag: <%@include file="... " %> this tag includes the equivalent servlet code of destination jsp to the equivalent servlet of source jsp
  3. Taglib Directive: <%@taglib %> It is used to include and use the JSTL tag library in our jsp file.

Apart from these we have more than 8 standard action tags:

 1. <jsp:useBean >
 2. <jsp:forward > etc.

Now coming to your question: You got confused on why the html and body tags are closed. And How can further code is rendered after html tag is closed.

First of all, what ever I told you above, that's very old style coding and completely out of trend. We are always suggested to use JSTL and other tags in jsp to keep the jsp files easy to read, clean and easy for maintenance. JSP files are for view layer and we shouldn't put any java code in jsp files. So the jsp file given by you is a total mess.

and the code written just after the closing of body and html tags is a scriptlet and it will be converted to equivalent java code inside the _jspService(req, rs) method that is meant for jsp equivalent servlet class.

   <% 
            return; 
        }//if
    }//try
    finally 
    {
        try
        {
            con.close();
        }catch(Exception e){}
    }//finally
    %>

And this code is simple text along with an expression tag(that again gives you an output to be displayed on web page in text format)

This is a Home Page <br/>
Welcome, <%=uname%>

[> Omitting the html, head, and body tags and writing text outside them is certainly allowed by the HTML specs. The underlying reason is that browsers have always sought to be consistent with existing web pages, and the very early versions of HTML didn't define those elements. When HTML 2.0 first did, it was done in a way that the tags would be inferred when missing.][source]1

For more detail see here

Upvotes: 3

Related Questions