100% found this document useful (5 votes)
55 views5 pages

JSP Login Form and Servlet Example

This document contains code for a JavaServer Pages (JSP) application that authenticates user credentials. It includes a JSP page with a form to input username and password. A servlet verifies the credentials against a database using JDBC. If valid, the servlet forwards the request to a "first.jsp" page, otherwise it includes an error message on the original JSP page. It also includes a Data package with a connection class to establish a database connection.

Uploaded by

seenu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (5 votes)
55 views5 pages

JSP Login Form and Servlet Example

This document contains code for a JavaServer Pages (JSP) application that authenticates user credentials. It includes a JSP page with a form to input username and password. A servlet verifies the credentials against a database using JDBC. If valid, the servlet forwards the request to a "first.jsp" page, otherwise it includes an error message on the original JSP page. It also includes a Data package with a connection class to establish a database connection.

Uploaded by

seenu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Jsp

<%@ page language="java" import="[Link].*" pageEncoding="ISO-8859-


1"%>
<%
String path = [Link]();
String basePath =
[Link]()+"://"+[Link]()+":"+[Link]
Port()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html>
<head>
<base href="<%=basePath%>">

<title>My JSP '[Link]' starting page</title>

<meta http-equiv="pragma" content="no-cache">


<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="[Link]">
-->

</head>

<body>
This is my JSP page. <br><h:form
action="[Link] method="Post"
rendered="true" >
<br><br>Userid&nbsp; <input type="text" name="t1"> <br>&nbsp;&nbsp;
<br>&nbsp;&nbsp;&nbsp;&nbsp; pass&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text"
name="t2"><br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input
type="submit" name="button1"value="submit"><br><br><br></h:form>
</body>
</html>

Servlet

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import Mypack.*;
import [Link].*;

public class Myservlet extends HttpServlet {


String s,s1,s2;
/**
* Constructor of the object.
*/
public Myservlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
[Link](); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method
equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

[Link]("text/html");
PrintWriter out = [Link]();

Connection con;
s=[Link]("t1");
s1=[Link]("t2");
Data d=new Data();
con=[Link]();
try
{
PreparedStatement ps=[Link]("select password
from user1 where username=? ");
[Link](1, s);
ResultSet rs=[Link]();
while([Link]())
{
s2=[Link](1);
}
}catch(SQLException x)
{
[Link](x);
}
if([Link](s2))
{
RequestDispatcher
rd=[Link]("/[Link]");
[Link](request, response);
}
else
{
RequestDispatcher
rd=[Link]("/[Link]");
[Link](request, response);
[Link]("user and password are invalid");
}
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}

Data pacakage

package Mypack;
import [Link].*;

public class Data {


Connection con;
public Connection cc()
{
try
{
[Link]("[Link]");
con=[Link]("Jdbc:Odbc: stu");

}catch(ClassNotFoundException c)
{
[Link]();
}catch(SQLException s){

[Link]();
}
return con;
}

First jsp

<%@ page language="java" import="[Link].*" pageEncoding="ISO-8859-


1"%>
<%
String path = [Link]();
String basePath =
[Link]()+"://"+[Link]()+":"+[Link]
Port()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html>
<head>
<base href="<%=basePath%>">

<title>My JSP '[Link]' starting page</title>

<meta http-equiv="pragma" content="no-cache">


<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="[Link]">
-->

</head>

<body>
This is my JSP page. sucesssssssssss <br>
</body>
</html>

You might also like