The Java layout is like this:
The Web. xml:
The login.jsp:
SUCCESS.JSP:
Acount.java:
The Login.java:
Package com.userinfo;
Import Java.io.File;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
public class Login extends HttpServlet {
/**
*
*/
Private static final long serialversionuid = 1L;
@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
TODO auto-generated Method Stub
Super.dopost (req, resp);
HttpSession session=req.getsession ();
Acount acount=new acount ();
String User_name=req.getparameter ("username");
String pass_wd=req.getparameter ("passwd");
try {
Class.forName ("Com.mysql.jdbc.Driver");
Connection conn=drivermanager.getconnection ("Jdbc:mysql://localhost/ncpss_ncpss", "root", "Wuxibo");
Statement st=conn.createstatement ();
ResultSet rs=st.executequery ("select * from UserInfo where username= '" +user_name+ "' and passwd= '" +pass_wd+ " ‘");
if (Rs.next ()) {
Acount.setusername (user_name);
ACOUNT.SETPASSWD (PASS_WD);
Session.setattribute ("acount", acount);
Req.getrequestdispatcher ("success.jsp"). Forward (req, resp);
Return
// }
Resp.sendredirect ("login.jsp");
Return
Acount acount=new acount ();
HttpSession session=req.getsession ();
String User_name=req.getparameter ("username");
String pass_wd=req.getparameter ("passwd");
try {
Class.forName ("Com.mysql.jdbc.Driver");
Connection conn=drivermanager.getconnection ("Jdbc:mysql://localhost/ncpss_ncpss", "root", "Wuxibo");
Statement st=conn.createstatement ();
ResultSet rs=st.executequery ("SELECT * from UserInfo");
while (Rs.next ()) {
String username=rs.getstring ("UserName");
String passwd=rs.getstring ("PassWd");
if (User_name.equals (userName) &&pass_wd.equals (passWd)) {
Acount.setusername (user_name);
ACOUNT.SETPASSWD (PASS_WD);
Session.setattribute ("acount", acount);
Req.getrequestdispatcher ("success.jsp"). Forward (req, resp);
Return
// }
// }
Resp.sendredirect ("login.jsp");
Return
Acount acount=new acount ();
HttpSession session=req.getsession ();
String User_name=req.getparameter ("username");
String pass_wd=req.getparameter ("passwd");
try {
Class.forName ("Com.mysql.jdbc.Driver");
Connection conn=drivermanager.getconnection ("Jdbc:mysql://localhost/ncpss_ncpss", "root", "Wuxibo");
Statement st=conn.createstatement ();
ResultSet rs=st.executequery ("SELECT * from UserInfo");
while (Rs.next ()) {
String username=rs.getstring ("UserName");
String passwd=rs.getstring ("PassWd");
if (User_name.equals (userName) &&pass_wd.equals (passWd)) {
Acount.setusername (user_name);
ACOUNT.SETPASSWD (PASS_WD);
Session.setattribute ("acount", acount);
Req.getrequestdispatcher ("success.jsp"). Forward (req, resp);
Return
}
}
Resp.sendredirect ("login.jsp");
Return
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
TODO auto-generated Method Stub
Super.doget (req, resp);
}
}
A simple Java login using JSP, servlet, JavaBean