Register the logon Module

Source: Internet
Author: User

In order to keep the main knowledge point clear, this module does not use Ajax technology, so it does not implement the registration time mark down function to verify whether the user name is occupied. Besides, the JS verification username and password of the client are omitted for compliance with the required functions. There are only two registration fields, namely the nickname and password. The nickname must be unique and the primary key is automatically increased. The database uses MySQL.

First, let's look at the file directory structure of the module:

The following is the source code of the three servlets:

Public class checkcodeservlet extends httpservlet {public void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("image/JPEG"); bufferedimage image = new bufferedimage (60, 20, bufferedimage. type_int_rgb); graphics G = image. getgraphics (); // system. out. println (G. getclass (); random r = new random (); G. setcolor (new color (R. nextint (1, 255), R. nextint (1, 255), R. nextint (255); G. fillrect (0, 0, 60, 20); G. setcolor (new color (0, 0); string number = string. valueof (R. nextint (99999); httpsession session = request. getsession (); Session. setattribute ("Number", number); G. drawstring (number, 5, 15); G. setcolor (new color (R. nextint (1, 255), R. nextint (1, 255), R. nextint (255); G. drawline (R. nextint (10), R. nextint (10), 40 + R. nextint (20), R. nextint (20); outputstream out = response. getoutputstream (); // compress the image into imageencoder encoder = required codec. createjpegencoder (out); // system. out. println (encoder. getclass (); encoder. encode (image );}}
Public class registerservlet extends httpservlet {public void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {request. setcharacterencoding ("UTF-8"); string nickname = request. getparameter ("nickname"); string Password = request. getparameter ("password"); string checkcode = request. getparameter ("checkcode"); httpsession session = request. getsession (); string n Umber = (string) Session. getattribute ("Number"); If (! Number. equals (checkcode) {request. setattribute ("checkcode_err", "Incorrect verification code"); Request. getrequestdispatcher ("register. JSP "). forward (request, response); return;} userdao Dao = new userdao (); try {user = Dao. findbynickname (nickname); If (user! = NULL) {request. setattribute ("register_err", "username occupied"); Request. getrequestdispatcher ("register. JSP "). forward (request, response);} else {user user2 = new user (nickname, password); Dao. save (user2); response. sendredirect ("login. JSP ") ;}} catch (exception e) {e. printstacktrace (); throw new servletexception (e );}}}
Public class loginservlet extends httpservlet {public void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {request. setcharacterencoding ("UTF-8"); string nickname = request. getparameter ("nickname"); string Password = request. getparameter ("password"); userdao Dao = new userdao (); try {user = Dao. findbynickname (nickname); If (user! = NULL & user. getPassword (). equals (password) {httpsession session = request. getsession (); Session. setattribute ("user", user); response. sendredirect ("welcome. JSP ");} else {request. setattribute ("login_err", "incorrect user name or password"); Request. getrequestdispatcher ("login. JSP "). forward (request, response) ;}} catch (exception e) {e. printstacktrace (); throw new servletexception (e );}}}

By the way, the source code of the four JSP files is attached:

Register. jsp:

<% @ Page contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <HTML> 

Login. jsp:

<% @ Page contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <HTML> 

Welcome. jsp:

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><%@ include file="sessionValidate.jsp" %>

Sessionvalidate. jsp:

<%Object obj=session.getAttribute("user");if(obj==null){    response.sendRedirect("login.jsp");}%>

The package file of the entire module has been uploaded to my resources and can be downloaded for free. The SQL script is too simple and is not provided. IDE is myeclipse. Although simple, cainiao can learn.

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.