A simple Java login using JSP, servlet, JavaBean

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.