Example of requestdispatcher
index.jsp
<html>
<head>
<title>MySqlLogin</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>
<body>
<div class="abc" id="f1">
<br>
<form method="POST" action="doLogin">
<table align="center">
<tr>
<td colspan=2 align=center>
Login
<hr>
</td>
</tr>
<tr>
<td>User Name : </td>
<td><input type="text" name="txtUsr"></td>
</tr>
<tr>
<td>Password :</td>
<td><input type="password" name="txtPwd"></td>
</tr>
<tr>
<td colspan=2 align=center>
<hr>
<input type="submit" value="Login">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<head>
<title>MySqlLogin</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>
<body>
<div class="abc" id="f1">
<br>
<form method="POST" action="doLogin">
<table align="center">
<tr>
<td colspan=2 align=center>
Login
<hr>
</td>
</tr>
<tr>
<td>User Name : </td>
<td><input type="text" name="txtUsr"></td>
</tr>
<tr>
<td>Password :</td>
<td><input type="password" name="txtPwd"></td>
</tr>
<tr>
<td colspan=2 align=center>
<hr>
<input type="submit" value="Login">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
admin.jsp
<html>
<head>
<title>MySqlLogin</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>
<body>
<div class="abc" id="f1">
welcome to admin
</div>
</body>
</html>
<head>
<title>MySqlLogin</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>
<body>
<div class="abc" id="f1">
welcome to admin
</div>
</body>
</html>
MySqlLogin.java
package bsr;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/doLogin")
public class MySqlLogin extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
String className= "com.mysql.jdbc.Driver";
String url= "jdbc:mysql://localhost:3306/test";
String user= "root";
String password= "admin";
String txtUsr= request.getParameter("txtUsr");
String txtPwd= request.getParameter("txtPwd");
Connection con;
PreparedStatement ps;
ResultSet rs;
try
{
Class.forName(className);
con= DriverManager.getConnection(url, user, password);
//String sql= "SELECT usr FROM login WHERE usr = '"+txtUsr+"' AND pwd = '"+txtPwd+"'";
String sql= "SELECT usr FROM login WHERE usr = ? AND pwd = ?";
ps=con.prepareStatement(sql);
ps.setString(1, txtUsr);
ps.setString(2, txtPwd);
rs= ps.executeQuery();
ServletContext context= getServletContext();
if(rs.next())
{
String n= rs.getString("usr");
RequestDispatcher rd= context.getRequestDispatcher("/admin.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher rd= context.getRequestDispatcher("/index.jsp");
out.println("<center><font color=red>invalid user name or password</font>");
rd.include(request, response);
}
con.close();
ps.close();
rs.close();
}
catch(SQLException sx)
{
out.println(sx);
}
catch(ClassNotFoundException cx)
{
out.println(cx);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/doLogin")
public class MySqlLogin extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out= response.getWriter();
String className= "com.mysql.jdbc.Driver";
String url= "jdbc:mysql://localhost:3306/test";
String user= "root";
String password= "admin";
String txtUsr= request.getParameter("txtUsr");
String txtPwd= request.getParameter("txtPwd");
Connection con;
PreparedStatement ps;
ResultSet rs;
try
{
Class.forName(className);
con= DriverManager.getConnection(url, user, password);
//String sql= "SELECT usr FROM login WHERE usr = '"+txtUsr+"' AND pwd = '"+txtPwd+"'";
String sql= "SELECT usr FROM login WHERE usr = ? AND pwd = ?";
ps=con.prepareStatement(sql);
ps.setString(1, txtUsr);
ps.setString(2, txtPwd);
rs= ps.executeQuery();
ServletContext context= getServletContext();
if(rs.next())
{
String n= rs.getString("usr");
RequestDispatcher rd= context.getRequestDispatcher("/admin.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher rd= context.getRequestDispatcher("/index.jsp");
out.println("<center><font color=red>invalid user name or password</font>");
rd.include(request, response);
}
con.close();
ps.close();
rs.close();
}
catch(SQLException sx)
{
out.println(sx);
}
catch(ClassNotFoundException cx)
{
out.println(cx);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
Subscribe to:
Post Comments (Atom)
Subjects
- WordPress
- Mobile Computing-4649303 Practical Solution
- Android Programming New Syllabus Theory
- PHP LAMP Question Bank
- PHP LAMP Theory
- Step by Step Android Example
- Android Practical
- Android Theory
- Android Question Bank
- Networking FON Practical
- Networking FON Theory
- OS Practical
- OS Theory
- HTML
- JavaScript
- J2EE WTAD Theory
- J2EE WTAD Question Bank
- J2EE WTAD Quick Guide
- J2EE WTAD GTU Papers
- J2EE WTAD Practical
- Python
- JAVA Theory
- JAVA Practical
- MIS
Categories
- Android (55)
- c (11)
- Configure Tomcat7 (2)
- CSS (3)
- Decryption (16)
- Difference (1)
- Encryption (16)
- Error Detection and Correction Techniques (3)
- FON (27)
- Framing Technic (2)
- install Tomcat (2)
- J2EE (29)
- JAVA (13)
- JavaScript (19)
- linux (8)
- OS (17)
- PHP (11)
- Protocol (3)
- SERVER SOCKET PROGRAMING (7)
- Servlet (13)
- shell script (33)
- unix (22)
- WTAD (34)
Blog Archive
-
▼
2013
(64)
-
▼
June
(25)
- Example of command line argument in C on Linux
- Example of command line argument in Java
- AES with CBC mode example in Java
- AES with CFM mode example in Java
- AES with ECB mode example in Java
- 3DES with CFM mode example in Java
- 3DES with CBC mode example in Java
- 3DES with ECB mode example in Java
- RSA Encryption Example in Java
- JSP JDBC Example
- example of preparedstatement in java
- example of page directive in jsp
- example of jsp scripting elements
- Example of requestdispatcher
- JSTL standard tag library Example
- DES with CBC mode example in Java
- DES with CFM mode example in Java
- DES with ECB mode example in Java
- One Time Pad Encryption Example using Java code
- S-Box Example using Java
- P-Box Example using Java
- Transposition cipher example using Java
- Mono-alphabetic Substitution Cipher example using ...
- Generalized Caesar Cipher example using java
- java socket programming example
-
▼
June
(25)
Total Pageviews
© BipinRupadiya.com. Powered by Blogger.
0 comments:
Post a Comment