1. stay struts2 There are two ways to use SercletAPI, One way to decouple , A way of coupling .

2. The decoupling method is to use ActionContext To achieve , It's completely decoupled servletAPI. ActionContext Can be used anywhere (action,service,dao).

Examples are as follows :

public String login(){
System.out.println(" user name :"+username+"\t password :"+password);
if("siggy".equals(username)&&"1111".equals(password)){
// adopt ActionContext obtain session
Map<String,Object> session = ActionContext.getContext().getSession();
session.put("currentUser", "siggy");
// Get all local
ActionContext.getContext().getApplication();
// Get the data in the request domain
ActionContext.getContext().get("request");
return Action.SUCCESS;
}
return "login";
}

3. Use by coupling ServletAPI. The way of coupling is SercletAPI The object of itself .

(a) . adopt ActionContext To achieve coupling .

Examples are as follows :

// Login processing method 
public String login(){
System.out.println(" user name :"+username+"\t password :"+password);
if("siggy".equals(username)&&"1111".equals(password)){
// adopt ActionContext obtain HttpServletRequest object
HttpServletRequest req = (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
req.getSession().setAttribute("currentUser", "siggy");
return Action.SUCCESS;
}
return "login";
}

(b) adopt ServletActionContext To get servletAPI:

Examples are as follows :

public String login(){
System.out.println(" user name :"+username+"\t password :"+password);
if("siggy".equals(username)&&"1111".equals(password)){
// adopt ServletActionContext obtain HttpServletRequest object
HttpServletRequest req = ServletActionContext.getRequest();
req.getSession().setAttribute("currentUser", "siggy");
return Action.SUCCESS;
}
return "login";
}

(c) By implementing SerletRuestAware Interface to get HttpServletRequest object

Examples are as follows :

public class User3Action implements ServletRequestAware{
private String username;
private String password; private HttpServletRequest req;
// Login processing method
public String login(){
System.out.println(" user name :"+username+"\t password :"+password);
if("siggy".equals(username)&&"1111".equals(password)){
req.getSession().setAttribute("currentUser", "siggy");
return Action.SUCCESS;
}
return "login";
}
public void setServletRequest(HttpServletRequest request) {
req = request;
}
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;
}
}

struts2 And ServletAPI More articles about

  1. Struts2 visit ServletAPI In three ways

    web Applications that need to be accessed ServletAPI, Usually only HttpServletRequest,HttpSession,ServletContext Three , These three interfaces represent jsp Built in objects request, ...

  2. struts2 visit ServletAPI And the way to get parameters

    One . visit ServletAPI In three ways The way 1: By making Action Class to implement the aware interface . At this point, the project depends on :servlet-api.jar. ServletRequestAware: perception HttpServlet ...

  3. 2018.11.21 struts2 get servletAPI And how to get parameters

    visit servletAPI The way The first one is : adopt ActionContext ( Key and common It's all about getting native objects ) principle Action To configure The imported configuration file Call the value... On the page The second kind : adopt ServletAction ...

  4. java And struts2 And ServletAPI

    In previous studies struts2 We can handle most of the problems . But if you want to store user login data into session in , There are two ways to open it ServletAPI. A way of decoupling , A way of coupling . 1. Decoupling mode Decoupling ...

  5. Struts2 And ServletAPI decoupling

    What is with Servlet API decoupling ? In order to avoid and servlet API Couple together , convenient Action Unit test , Struts2 Yes HttpServletRequest,HttpSession, and Serv ...

  6. 【 thorough Struts2】 obtain ServletAPI In three ways

    One : obtain servletAPI Three ways In traditional Web In development , Often used Servlet API Medium HttpServletRequest.HttpSession and ServletContext.Strut ...

  7. Struts2 A classic introduction to

    One :Struts2 The origin and background of In understanding Struts2 Let's talk about it first Struts1, We all know that for a long time , be-all MVC In the frame ,Struts1 He is in the position of a superstar , No matter from the market point of view and the use of ...

  8. Struts2 introduction

    One .Struts2 Introductory cases ① introduce jar package ② stay src Create struts.xml The configuration file <?xml version="1.0" encoding="UTF-8&q ...

  9. 【Java EE Study 35 On 】【strus2】【 Type converter 】【struts2 and Servlet API decoupling 】【 Internationalization issues 】【 The problem of garbled resource files has been solved 】

    One . Type converter 1. There are three common built-in modules action in , There are three common built-in modules name Property with the same value , Provide get and set Method ,struts2 You can use the reflection mechanism , Get the corresponding content from the page package com.kdyzm ...

Random recommendation

  1. How to be in vim Delete empty lines from the list ?

    Delete blank lines , Enter bottom line mode :g/^$/d ^ Represents the first column $ Represents the tail column d On behalf of the delete g Represents a global replacement

  2. 【Selenium2+Python】 location

    location Frame driver.switch_to_frame("frameID") Multi-window switching # Get the current window nowhandle = driver.current_window_ ...

  3. oracle Data objects in

    oracle The data objects in are tables . View . Indexes . Sequence, etc Table related operations 1. Create table Mode one : Mode two :create table person( create table person1 id number(1 ...

  4. [GeoServer]Openlayers Simple call

    Openlayers Demo: <html> <head> <title>OpenLayers Example</title> <script ...

  5. 8 A must have PHP Function development ( turn )

    done PHP Developers should know ,PHP There are many built-in features in , Master them , Can help you in doing PHP It's easier to develop , This article will share 8 It's a necessary part of development PHP function , All of them are very practical , I hope you all PHP Developers can master . 1. Pass on ...

  6. Recursive implementation of single linked list inversion (Reversing a Linked List in Java, recursively)

    from Reversing a Linked List in Java, recursively There's code in one reply that spells it out, but you ...

  7. cursor The shape of the

    Example:CSS Mouse hand effect <a href="#" style="cursor:hand">CSS Mouse hand effect </a> Exam ...

  8. Mariadb Redis Configuration usage

    Mariadb Mysql Configuration usage CentOS 7 Mariadb Learning from stay linux How to install the software on the computer yum install Search online rpm Format package , Automatic dependency processing , download , install ​ ( Alibaba cloud ...

  9. turn ://Oracle in User and Schema The difference and connection

    Reading today Oracle When it comes to official documents , Read schema Basic concepts of , This raises a question for me :user and schema What's the difference between the two ? For a deeper understanding of the differences and connections between the two , Here is the official document about us ...

  10. Luogu2164 SHOI2007 traffic network expect 、BFS、 A topological sort

    Portal Isn't the subject difficult First, let's enumerate the points $i$, Find out the shortest path from all other points to this point And then we built the shortest path this time $DAG$ Topological ordering on the inverse graph of . Let's say we get to a point $j$, spot $j$ The flow of people is $t_j$, spot $ ...