Struts2 访问上下问对象
发布日期:2021-06-30 10:51:57 浏览次数:2 分类:技术文章

本文共 4648 字,大约阅读时间需要 15 分钟。

MyEclipse结构如下:

在my包下有2个类,

一个是Servlet是LoginServlet.java

package my;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class LoginServlet extends HttpServlet {	/**	 * Constructor of the object.	 */	public LoginServlet() {		super();	}	/**	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String user=request.getParameter("user"); String password=request.getParameter("password"); if("123456".equals(password)){ HttpSession hs=request.getSession(); hs.setAttribute("user", user); //重定向到index.jsp String contextPath=request.getContextPath(); response.sendRedirect(contextPath+"/index.jsp"); } else{ response.sendError(400, "密码错误!"); } } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here }}

另外一个是Test2Action.java

package my;import com.opensymphony.xwork2.ActionSupport;public class Test2Action extends ActionSupport{	String reason = "A test message!";		int result = 0;		@Override	public String execute() throws Exception	{		result = 100;				return "success";	}	public String getReason()	{		return reason;	}	public void setReason(String reason)	{		this.reason = reason;	}	public int getResult()	{		return result;	}	public void setResult(int result)	{		this.result = result;	}}

struts.xml配置如下

/Test2_success.jsp

index.jsp配置

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page	
首页

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'login.jsp' starting page    	
用户名:
密码:

Test2_success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'Hello.jsp' starting page    	

测试1 : #parameters

( 相当于 request.getParameter("") )
result:

测试2 : #session

( 相当于 session.getAttribute("") )
result:
另外一种写法 :

测试3 : #reqeust

#request['xxx'] 相当于 request.getAttribute("xxx")
#parameters['xxx'] 相当于 request.getParameter("xxx")
两者不同

测试4 :#action

result:
相当于:

测试5 :if判断

够大!
太小了!

测试6 :if判断

未登录
,您好!

转载地址:https://it1995.blog.csdn.net/article/details/79346951 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:struts2在Action中访问Session,管理员删除用户
下一篇:Struts2 - Action 基础用法

发表评论

最新留言

很好
[***.229.124.182]2024年04月11日 01时59分11秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章