Reputation: 53
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
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-
Hence:
Apart from these 3 scripting tags, we have 3 directive tags in jsp
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