Program 10
Program 10
Objective : Write a JSP which insert the details of the 3 or 4 users who register
with the web site by using registration form. Authenticate the user when he
submits the login form using the user name and password from the database
Theory :
JSP scripting elements let you insert Java code into the servlet that will be
generated from the current JSP page. There are three forms:
1. Expressions of the form <%= expression %> that are evaluated and
inserted into the output,
2. Scriptlets of the form <% code %> that are inserted into the
servlet's service method, and
3. Declarations of the form <%! code %> that are inserted into the body of
the servlet class, outside of any existing methods.
Each of these is described in more detail below.
JSP Expressions
A JSP expression is used to insert Java values directly into the output. It has the
following form:
<%= Java Expression %>
The Java expression is evaluated, converted to a string, and inserted in the page.
This evaluation is performed at run-time (when the page is requested), and thus
has full access to information about the request. For example, the following
shows the date/time that the page was requested:
Current time: <%= new java.util.Date() %>
To simplify these expressions, there are a number of predefined variables that
you can use. These implicit objects are discussed in more detail later, but for the
purpose of expressions, the most important ones are:
• request, the HttpServletRequest;
• response, the HttpServletResponse;
• session, the HttpSession associated with the request (if any); and
• out, the PrintWriter (a buffered version of type JspWriter) used to send
output to the client.
JSP Scriptlets
If you want to do something more complex than insert a simple expression,
JSP scriptlets let you insert arbitrary code into the servlet method that will be
built to generate the page. Scriptlets have the following form:
<% Java Code %>
Scriptlets have access to the same automatically defined variables as
expressions. So, for example, if you want output to appear in the resultant page,
you would use the out variable.
<%
String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);
%>
Note that code inside a scriptlet gets inserted exactly as written, and
any static HTML (template text) before or after a scriptlet gets converted to print
statements. This means that scriptlets need not contain complete Java
statements, and blocks left open can affect the static HTML outside of the
scriptlets.
JSP Declarations
A JSP declaration lets you define methods or fields that get inserted into the
main body of the servlet class (outside of the service method processing the
request). It has the following form:
<%! Java Code %>
Since declarations do not generate any output, they are normally used in
conjunction with JSP expressions or scriptlets. For example, here is a JSP
fragment that prints out the number of times the current page has been
requested since the server booted (or the servlet class was changed and
reloaded):
<%! private int accessCount = 0; %>
PROGRAM:
Login.html:
<!--Home.html-->
<html> <body>
<center><h1>XYZ Company Ltd.</h1></center>
<table border="1" width="100%" height="100%">
<tr>
<td valign="top" align="center"><br/>
<form action="auth.jsp"><table>
<tr>
<td colspan="2" align="center"><b>Login Page</b></td>
</tr>
<tr>
<td colspan="2" align="center"><b> </td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="user"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="LogIN"/></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Auth.jsp:
<%@page import="java.sql.*;"%>
<html>
<head>
<title>
This is simple data base example in JSP</title>
</title>
</head>
<body bgcolor="yellow">
<%!String uname,pwd;%>
<%
uname=request.getParameter("user");
pwd=request.getParameter("pwd");
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@195.100.101.158:1521:CCLAB","scot
t","tiger");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select name,password from personal where
name='"+uname+"' and password='"+pwd+"'");
if(rs.next())
{
out.println("Authorized person");
}
else
{
out.println("UnAuthorized person");
}
con.close();
}
catch(Exception e){out.println(""+e);}
%>
</body>
</html>
OUTPUT: