Java Jsp+servlet+mysql Implementation Login Web Design

Source: Internet
Author: User

The following files are involved:


1, login page login.jsp

2, successful jump page success.jsp

3, failed jump page fail.jsp

4,servlet Processing Class Logintestservlet.java

5, config file Web. xml


---------------------------------------------look at the code in turn--------------------------------------------

Logintestservlet.java Content

Package Com.test;import Java.io.ioexception;import Java.sql.drivermanager;import java.sql.resultset;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;import Com.mysql.jdbc.connection;import com.mysql.jdbc.statement;/** * servlet Implementation class Logintestservlet */@WebServlet ("/logintestservlet") public class Logintestservlet extends           HttpServlet {private static final long serialversionuid = 1L;            /** * @see httpservlet#httpservlet () */public Logintestservlet () {super (); }/** * @see Httpservlet#doget (httpservletrequest request, httpservletresponse response) */protected void Doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {doPost (Request, Response);} /** * @see Httpservlet#dopost (HttpServletRequest reQuest, httpservletresponse response) */protected void DoPost (HttpServletRequest request, httpservletresponse response) Throws Servletexception, IOException {response.setcontenttype ("text/heml;charset=gb2312"); Request.setcharacterencoding ("gb2312"); String result = ""; String username = request.getparameter ("username"); String PSW = request.getparameter ("password"); if (username = = "" | | Username = = NULL | | Username.length () >20) {try {result = "Please enter a user name (cannot exceed 20 characters)"; Request.setattribute ("message", result); Response.sendredirect ("login.jsp"); return;} catch (Exception e) {e.printstacktrace ();}} if (PSW = = "" | | PSW = = NULL | | Psw.length () >) {try {result = "Please enter a password (cannot exceed 20 characters)"; Request.setattribute ("message", result); Response.sendredirect ("login.jsp"); return;} catch (Exception e) {e.printstacktrace ();}} Enlist the JDBC driver try {class.forname ("Com.mysql.jdbc.Driver"),} catch (Exception e) {System.out.print ("Class not Found Exception ");} Link urlstring url = "Jdbc:mysql://localhost:3306/demo"; Connection Conn = NULL; Statement stmt = null; ResultSet rs = null;try {conn = (Connection) drivermanager.getconnection (URL, "root", "root"); stmt = (Statement) Conn.createstatement (); String sql = "SELECT * from UserInfo where username= '" +username+ "' and userpsw= '" +psw+ "'"; rs = stmt.executequery (sql);} catch (Exception e) {e.printstacktrace ();}    HttpSession session = Request.getsession (); Session.setattribute ("username", username); try {if (Rs.next ()) {    Session.setattribute ("Age", rs.getstring ("Age"));    Session.setattribute ("Sex", rs.getstring ("Sex"));    Session.setattribute ("Weight", rs.getstring ("weight"));    Response.sendredirect ("success.jsp"); return;} else{session.setattribute ("message", "User name or password does not match.")    ");    Response.sendredirect ("fail.jsp"); return;}} catch (Exception e) {e.printstacktrace ();}}}


login.jsp Content

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
success.jsp Content

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
fail.jsp Content

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >


-------------------------------------------------------Configuration Database------------------------------------------------------

The MySQL database needs to create a database demo, where the CREATE TABLE UserInfo table statement is:
CREATE Databasedemo;
Use demo
CREATE TABLE userInfo (ID int not NULL primary key,username char (a) not NULL,USERPSW char (a), age int, weight int, Sex enum (' M ', ' F '));
  
need to place the MySQL-connected jar package in the project's Lib directory

---------------------------------------------Gorgeous split-line----------------------------------------

Last web. XML content

<?xml Version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http ://java.sun.com/xml/ns/javaee "xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/ Javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 "><display-name>webDemo</display-name>< welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</ welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</ welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</ Welcome-file></welcome-file-list> 
<servlet><servlet-name>LoginTestServlet</servlet-name><servlet-class> Com.test.logintestservlet</servlet-class></servlet><servlet-mapping><servlet-name> logintestservlet</servlet-name><url-pattern>/logintestservlet</url-pattern></ Servlet-mapping></web-app>


Java Jsp+servlet+mysql Implementation Login Web Design

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.