Struts2 Study Notes (2) -- Obtain logon information and calculate the number of online users

Source: Internet
Author: User
Purpose: 1. Click the "login" hyperlink to go To the logon page. 2. Enter the logon information, submit the form, and save the user information to the session.
3. display the user name and calculate the number of online users
4. Click the "logout" hyperlink to reduce the number of online users by one and invalidate the session.
Struts2 implementation: 1. Configure the Web. xml file
<?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_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>struts2-LoginAndLogout</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>  <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></web-app>

2. Import the basic jar package required by struts2

3. Compile the action class
Package COM. lewa. action; import Java. util. map; import Org. apache. struts2.dispatcher. sessionmap; import Org. apache. struts2.interceptor. applicationaware; import Org. apache. struts2.interceptor. sessionaware; public class loginaction implements sessionaware, applicationaware {Private Static final string success = "success"; Private Static final string logout = "logout "; // Private string username corresponding to the attribute in the form field; // javab EAN-style public void setusername (string username) {This. username = username;} Public String execute () {// Save the user information to the session. // 1. get the session and implement it through the sessionaware interface // 2. obtain logon information // 3. save the logon information to the session. put ("username", username); // number of online users + 1 // 1. obtain the number of online users. Obtain integer COUNT = (integer) application from application. get ("count"); If (COUNT = NULL) {COUNT = 0 ;}// make the current number of online users + 1 count ++; application. put ("count", count); Return success;} public s Tring logout () {// number of online users-1 // 1. obtain the number of online users integer COUNT = (integer) application. get ("count"); // 2. -1if (count! = NULL & count! = 0) {count --; application. put ("count", count);} // invalidate the session (sessionmap) session ). invalidate (); Return logout;} private Map <string, Object> session; @ overridepublic void setsession (Map <string, Object> session) {// todo auto-generated method stubthis. session = session;} private Map <string, Object> application; @ overridepublic void setapplication (Map <string, Object> application) {// todo auto-generated method stubthis. application = Application ;}}

4. configure action in struts. xml
<? 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> <! -- Modify default as a constant. the action configured in properties. extension --> <constant name = "struts. action. extension "value =" action, do, "/> <package name =" loginandlogout "extends =" struts-Default "> <! --> Action class: actionsupport by default> execution method: Execute> result: success by default. You can omit the preceding three statements. --> <! -- Enter the action configuration on the logon page --> <action name = "login-UI" class = "com. opensymphony. xwork2.actionsupport "method =" execute "> <result name =" success ">/login. JSP </result> </Action> <! -- Log on to the action configuration of the submitted Form --> <action name = "login" class = "com. lewa. action. loginaction "> <result>/login-success.jsp </result> </Action> <! -- Logout action configuration --> <action name = "logout" class = "com. lewa. action. loginaction "method =" logout "> <result name =" logout ">/logout-success.jsp </result> </Action> </package> </struts>

5. The corresponding JSP page index. jsp
<%@ 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">

Login. jsp
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 

Login-success.jsp
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 

Logout-success.jsp
<% @ 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"> <HTML> 

6. User request extension> ORG/Apache/struts2/Default. properties: some constants of the struts2 application are configured.
> Struts. Action. Extension = action, which indicates configuring struts2 to accept the request extension (by default, it is. Action or no extension)
> In struts. in the XML file, modify default as a constant. the constant configured by properties: <constant name = "struts. action. extension "value =" action, do, "> </constant>
7. Web application structure directory





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.