JSP_TOMCAT_MYSQL_ Registration authentication User;

Source: Internet
Author: User
Tags stmt git clone

This article originates from: Http://blog.csdn.net/svitter

Resources Download:

GitHub

git clone https://github.com/Svtter/JSP-tomcat-mysql


The servlet is used:

Xml:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee/http Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "version=" 3.1 ">  <display-name>JSP_servlet_javabean< /display-name><servlet><description> configuration for user login servlet</description><display-name> Loginservlet</display-name><servlet-name>loginservlet</servlet-name><servlet-class> Loginservlet</servlet-class></servlet>  <servlet-mapping><servlet-name>loginservlet </servlet-name><url-pattern>/LoginServlet</url-pattern></servlet-mapping>< welcome-file-list>    <welcome-file>login.html</welcome-file>  </welcome-file-list> </web-app>

Four Java files:

loginservlet.java://for verification:

Import Java.io.*;import Javax.servlet.*;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Bean. Connectionbean;import bean.userbean;/** * @author svitter * */@SuppressWarnings ("Serial") public class Loginservlet Extends HttpServlet {UserBean user = new UserBean (); Connectionbean Connbean = new Connectionbean ();p ublic void DoPost (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {String checklogin; String username = request.getparameter ("login"); String Password = request.getparameter ("password"); Checklogin = connbean.checkuser (username, password); if ( Checklogin.equals ("Success")) {User.setusername (username); Request.setattribute ("User", user); Getservletconfig (). Getservletcontext (). Getrequestdispatcher ("/loginsuccess.jsp"). Forward (request, response);} else {request.setattribute ("Checklogin", Checklogin), Getservletconfig (). Getservletcontext (). Getrequestdispatcher ("/logincheck.jsp "). Forward (request, response);} public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, ioexception{ String username = request.getparameter ("login"); String Password = request.getparameter ("password"); Connbean.adduser (username, password); Getservletconfig (). Getservletcontext (). Getrequestdispatcher ("/login.html"). Forward (request, response);}}

Bean. getconnection.java://for connecting to database, database configuration file property.conf

Package Bean;import java.io.*;import java.util.properties;import java.sql.*;p ublic class Getconnection {private static Properties P;static{try{p = new properties (); InputStream is = GetConnection.class.getResourceAsStream ("property.conf" );p. Load (IS); Is.close ();} catch (Exception e) {e.printstacktrace ();}} public static string GetProperty (String key) {return P.getproperty (key);} static string driver = Getconnection.getproperty ("driver"); static string url = Getconnection.getproperty ("url");  String name = Getconnection.getproperty ("user"); static string pass = Getconnection.getproperty ("password"); Static{try{class.forname (driver);} catch (ClassNotFoundException e) {e.printstacktrace ();}} public static Connection getconnection () throws SQLException {return drivermanager.getconnection (URL, name, pass);}}

Property.conf:

Driver=com.mysql.jdbc.driverurl=jdbc:mysql://localhost:3306/usersuser=rootpassword=root

Bean. connectionbean.java://for database operations

Package Bean;import java.sql.*;p ublic class Connectionbean {private Connection con;public Connectionbean () {Try{con = Get Connection.getconnection ();} catch (Exception e) {e.printstacktrace ();}} Add information to the database public boolean addUser (string name, string password) {try{preparedstatement pstmt = con.preparestatement (" insert into ur (name, passwd) VALUES (?,?) "); Pstmt.setstring (1, name);p stmt.setstring (2, password);p stmt.execute (); return true;} catch (Exception e) {e.printstacktrace (); return false;}} Public ResultSet GetUser (String name) {try {Statement stm = Con.createstatement (); ResultSet result = Stm.executequery ("select * from ur where name= '" +name+ "'"); return result; catch (Exception e) {e.printstacktrace (); return null;}} public string CheckUser (string name, String passwd) {string Checkuser=null;try{statement stm = Con.createstatement (); ResultSet result = Stm.executequery ("select * from ur where name= '" +name+ "'"); if (Result.next () ==false) {CheckUser = "No US Er ";} Else{if (result.getstring ("passwd"). EqualS (passwd)) {CheckUser = "Success";} Else{checkuser = "wrong passwd";}} catch (Exception e) {e.printstacktrace ();} return checkUser;}}


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.