Login Registration of Information Management system

Source: Internet
Author: User

completed the database connection in the advanced application of the previous database, then completed the login registration of the information management system.
Logging in is actually querying the database for user names and passwords that are entered by the user on the page. Check out and then return to the page, login successfully display the corresponding page, failed to display the corresponding page.
Registration is actually the use of SQL statements to insert data into the database, insert successfully display the corresponding page, failed to display the corresponding page.
1. First create the user class.

Package Cn.edu.hpu.model;public class User {private static int id;private static string username;private static string pas sword;public int getId () {return ID;} public void setId (int id) {user.id = ID;} Public String GetUserName () {return username;} public void Setusername (String username) {user.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {user.password = password;}} <span style= "font-family:kaiti_gb2312;" ></span>
say here a build package rule, General package best built cn.edu.cn .... This type. 2. Now start writing the interface class, which is a series of operations from the database.
first define the interface。

Package Cn.edu.hpu.service;import Cn.edu.hpu.model.user;public Interface Usermanager {public boolean registeuser (User User);p ublic boolean checklogin (String username,string  password); <span style= "font-family:kaiti_gb2312;" ></span>
immediately followed by the implementation class to write the interface.

Package Cn.edu.hpu.service;import Java.sql.connection;import Java.sql.resultset;import java.sql.SQLException; Import Com.mysql.jdbc.preparedstatement;import Cn.edu.hpu.model.user;import Cn.edu.hpu.util.util;public class Usermanagerimp implements Usermanager {@Overridepublic boolean registeuser (user user) {Boolean flag=false; String sql= "INSERT into haha (username,password) VALUES (?,?)"; Connection conn=util.getconnection ();//Connect database try {preparedstatement PST = (preparedstatement) conn.preparestatement ( SQL); Pst.setstring (1,user.getusername ()); Pst.setstring (2,user.getpassword ()); Pst.executeupdate (); Flag=true;}  catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return flag;} @Overridepublic boolean Checklogin (string username, string password) {Boolean flag=false; String sql= "SELECT * from haha where username=? and password=? "; Connection conn=util.getconnection (); try {PreparedStatement pst= (preparedstatement) conn.preparestatement (SQL); Pst.setstring (1,username);p st.setstring (2,password); ResultSet rs=pst.executequery ();//Traverse result set if (Rs.next ()) {flag=true;}} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return flag;}} <span style= "font-family:kaiti_gb2312;" ></span>
This completes a series of operations from the database.
3. Start writing Sevlet now. The servlet assumes contact with the page and receives the forwarding. Many times it is the submission address of a JSP page such as a form. Servlet: is a server-side Java application that has platform-and protocol-independent features that can generate dynamic Web pages. It serves as the middle tier of a customer request (a Web browser or other HTTP client program) and a server response (a database or application on an HTTP server). Servlets are Java applications located on the server side of the Web server, unlike traditional Java applications launched from the command line, which are loaded by the Web server. The Web server must contain the Java Virtual machine servlet life cycle that supports the servlet.
1) First is the login Servlet

Package Cn.edu.hpu.servlet;import Java.io.ioexception;import Java.io.printwriter;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;import Cn.edu.hpu.service.usermanager;import cn.edu.hpu.service.UserManagerImp; public class Checklogin extends HttpServlet {/** * */private static final long serialversionuid = 1l;public void Doget (Ht Tpservletrequest request, HttpServletResponse response) throws Servletexception, IOException {doPost (Request, response );//whether doPost or doget, dopost}public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {request.setcharacterencoding ("Utf-8"); Response.setcharacterencoding ("Utf-8");// Get username,passwordstring username = request.getparameter ("username");//Get username,passwwordstring password from the page = Request.getparameter ("password"); Usermanager MNG = new UsermAnagerimp (); Boolean flag=mng.    Checklogin (username, password); Get the user verification code and system generated verification code string Checkcode = Request.getparameter ("Checkcode"); String piccode = (string) request.getsession (). getattribute ("Piccode"); SYSTEM.OUT.PRINTLN ("---" +checkcode+ "pic---" +piccode); if (Flag==true&&piccode.equalsignorecase (Checkcode    ) {Studentdaoservlet servlet=new studentdaoservlet (); Servlet.dopost (request, response); HttpSession session=request.getsession ();//Get Session Object Session.setattribute ("username", username);} else {PrintWriter out = Response.getwriter (); Out.write ("incorrect!");}}} <span style= "font-family:kaiti_gb2312;" ></span>
2) is followed by a registered servlet.
Package Cn.edu.hpu.servlet;import Java.io.ioexception;//import Java.io.printwriter;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Cn.edu.hpu.model.User ; Import Cn.edu.hpu.service.usermanager;import Cn.edu.hpu.service.usermanagerimp;public class Register extends HttpServlet {/** * */private static final long serialversionuid = 1l;public void doget (HttpServletRequest request, Httpse Rvletresponse response) throws Servletexception, IOException {doPost (request,response);} public void DoPost (H Ttpservletrequest request, HttpServletResponse response) throws Servletexception, IOException { Request.setcharacterencoding ("Utf-8"); String username=request.getparameter ("Username"); String password=request.getparameter ("Password"); User U=new User (); u.setusername (Username); U.setpassword (Password); Usermanager um=new Usermanagerimp (); Boolean flag=um. Registeuser (U); if (flAG) {response.sendredirect ("chenggong.jsp");}}} <span style= "font-family:kaiti_gb2312;" ></span>
4. Finally, the JSP page1) JSP for login page

<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >2) JSP to register the page

<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >In the form form on the JSP page there is a setting for action, that is, the submission address of the form (which servlet is submitted for processing), the method setting (Get,post), and the setting of name in input to the servlet. 5. The above 1 2 3 4 steps are made from the bottom up. Now let's upload the data from the page to the bottom. 
when we fill in the form on the page of data, the servlet to receive, get the corresponding string, from then create the elder sister de object, passed the parameter Username,password, call the corresponding method to implement the interface, After the completion of the interface, according to the return value to determine the correctness of the whole process, and then according to the correct or not jump to the corresponding page. In fact, the entire process is from the page to the bottom of the database, from the bottom of the database back to the page. The most important thing is to implement it through interfaces and Servlets. Registration is the same principle.
This completes the login registration of the information management system.

Login registration for information Management system

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.