"Java EE Core Development Learning Note 003" STRUTS2 Construction and connection database realizes user registration and login

Source: Internet
Author: User

Build Struts2 is the focus, build this framework some trouble, encountered a lot of problems, finally one by one settlement, written here, to make memo.

1. First to download some components of struts2, the version I downloaded is struts-2.3.28.1,myeclipse is version:2014 version. Due to the continuous upgrade of the version, there are some differences in the construction process, compared with the older version of the past, but the problem is not very large. Here is the address of the downloaded struts2: HTTP://STRUTS.APACHE.ORG/DOWNLOAD.CGI#STRUTS25

2. Unzip the downloaded components we can see such a directory,


In apps is an example of struts2, Docs is a reference document for Struts2, Lib is the struts2 of all component jar packages, and SRC is the source code for STRUTS2. We opened the apps decompression Struts2-blank.war, found the following components in the extracted Struts2-blank\web-inf\lib file,


In addition, because we need to connect to the database, so find a connection to the MySQL jar, I use Mysql-connector-java-5.1.13-bin.jar, a new Web project, to copy these components to the established Web project under the web-inf/ In the Lib directory, these components are actually compiled class files.

3. It is also a critical step to configure the Web. xml file, and the server will initialize it according to this file when the server is started. And the struts2 and struts1 boot is very different, it is started through the filter, and Struts1 is loaded by the servlet to start, so the configuration of Web. XML is as follows.

<?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/j2ee" xmlns:web= "Http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation= "http ://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd HTTP://JAVA.SUN.COM/XML/NS/J2EE Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "id=" Webapp_9 "version=" 2.4 "> <display-name>struts Blank </display-name> <filter> <filter-name>struts2</filter-name> <filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter> < filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </ filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </ Welcome-file-list></web-app>

4. The next step is to configure the Struts.xml file. This file is like a filter, the user sent the request to pass through the file filtering, according to the user request in the requested processing type, to select the appropriate processing logic. Our Struts.xml file here is configured as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "http://struts.apache.org/dtds/ Struts-2.3.dtd "><struts><package name=" backed "namespace="/backed "extends=" Struts-default ">< Action name= "Login" class= "com.struts2_test.action.UserAction" method= "Login" ><result name= "Loginout" >/ Error.jsp</result><result name= "Loginin" >/welcome.jsp</result></action><action name= " Regist "class=" com.struts2_test.action.UserAction "method=" regist "><result name=" error ">/error.jsp</ Result><result name= "Success" >/welcome.jsp</result></action></package></struts >

In Struts.xml, the package is used to manage all kinds of action. The namespace in the package tag specifies the namespace of the bundle. Extends is that the current package inherits from the Struts-default package, This package can be found in the Struts-default.xml file in Struts2-core-2.3.28.1.jar, which is defined as abstract and cannot contain an action. Each action tag in the package is mapped to a logical processing class, and name specifies the name of the action, which is combined with the namespace of the package to directly access the logical processing class. class specifies the location of the logical processing class, which specifies a method for logical processing in a logical processing class, which is important, and it has a fixed return type, which is string. The result tag is the string returned by the Execute method, based on the logic, to enter the specified page according to the returned string. Here must be the address of the jump to write clearly, understand. Otherwise the browser will report a 404 error.

5. According to the configuration of struts.xml, we need to set up four JSP pages in Webroot directory for regist.jsp, login.jsp, welcome.jsp and error.jsp respectively. Create two Java class files Dao.java and Useraction.java in the SRC directory. It is important to note that Useraction inherits from the Actionsupport class.

The directory structure of the project is:


Their source code is:

REGIST.JSP:

<%@ 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" >

LOGIN.JSP:

<%@ 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" >

WELCOME.JSP:

<%@ 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" >

ERROR.JSP:

<%@ 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" >

Dao.java:

Package Com.struts2_test.dao;import java.sql.*;p ublic class Dao {Connection con = null; Statement stat = null; ResultSet rs = null;public Dao () {try {class.forname ("com.mysql.jdbc.Driver"); con = drivermanager.getconnection ("JDBC: Mysql://localhost:3306/struts2_test "," root "," root "); stat = Con.createstatement ();} catch (Exception e) {///Todo:handle Exceptioncon = null;}} Public ResultSet executeQuery (String sql) {try {rs = stat.executequery (sql)} catch (Exception e) {//Todo:handle Excepti Onrs = null;} return RS;} public int executeupdate (String sql) {try {stat.executeupdate (SQL); return 0;} catch (Exception e) {//Todo:handle Excepti on}return-1;}}

Useraction.java:

Package Com.struts2_test.action;import Com.opensymphony.xwork2.actionsupport;import Com.struts2_test.dao.Dao; Import java.sql.*;p ublic class Useraction extends Actionsupport {private dao DAO = new Dao ();p rivate String Username;priva Te string Password;public string GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public String Login () {string-sql = "SELECT * from Tb_user where t_username= '" + getusername () + "' and T_userpassword = '" +g Etpassword () + "'"; ResultSet RS = dao.executequery (sql), try {if (Rs.next ()) {return ' Loginin ';} return "Loginout";} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace (); return "Loginout";}} Public String Regist () {string-sql = "INSERT into Tb_user (T_username,t_userpassword,t_ischeck) VALUES ('" +getusername () + "', '" +getpassword () + "', ' 0 ')"; int i = dao.executeupdate (sql); if (i > -1) {return "Success";} return "Error";}}

Finally look at our database struts2_test:

After these steps, start your server, enter the address of the login interface in the Address bar, a simple struts2 framework is built.

CSDN Download: http://download.csdn.net/detail/davebobo/9533847


"Java EE Core Development Learning Note 003" STRUTS2 Construction and connection database realizes user registration and login

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.