You are on page 1of 5

Jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-


1"%>
<%
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServer
Port()+path+"/";
%>

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


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

<title>My JSP 'MyJsp.jsp' 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="styles.css">
-->

</head>

<body>
This is my JSP page. <br><h:form
action="http://localhost:8080/Design/servlet/Myservlet" 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 java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Mypack.*;
import java.sql.*;

public class Myservlet extends HttpServlet {


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

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // 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 {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

Connection con;
s=request.getParameter("t1");
s1=request.getParameter("t2");
Data d=new Data();
con=d.cc();
try
{
PreparedStatement ps=con.prepareStatement("select password
from user1 where username=? ");
ps.setString(1, s);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
s2=rs.getString(1);
}
}catch(SQLException x)
{
System.out.println(x);
}
if(s1.equals(s2))
{
RequestDispatcher
rd=request.getRequestDispatcher("/first.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher
rd=request.getRequestDispatcher("/MyJsp.jsp");
rd.include(request, response);
out.print("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 java.sql.*;

public class Data {


Connection con;
public Connection cc()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("Jdbc:Odbc: stu");

}catch(ClassNotFoundException c)
{
c.printStackTrace();
}catch(SQLException s){

s.printStackTrace();
}
return con;
}

First jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-


1"%>
<%
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServer
Port()+path+"/";
%>

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


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

<title>My JSP 'first.jsp' 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="styles.css">
-->

</head>

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

You might also like