日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Struts2 访问上下问对象

發布時間:2025/3/15 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2 访问上下问对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/*** The doGet method of the servlet. <br>** 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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the GET method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** The doPost method of the servlet. <br>** 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.jspString contextPath=request.getContextPath();response.sendRedirect(contextPath+"/index.jsp");}else{response.sendError(400, "密碼錯誤!");}}/*** Initialization of the servlet. <br>** @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;@Overridepublic 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配置如下

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts><package name="main" extends="struts-default"><action name="Test2" class="my.Test2Action" > <result name="success"> /Test2_success.jsp</result></action> </package></struts>


index.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"> <html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>首頁</body> </html>


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"> <html><head><base href="<%=basePath%>"><title>My JSP 'login.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form method="post" action="servlet/LoginServlet">用戶名:<input type="text" name="user"><br><br>密碼:<input type="password" name="password"><br><br><input type="submit" name="登錄"><br><br></form></body> </html>

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+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'Hello.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><h1> 測試1 : #parameters </h1>( 相當于 request.getParameter("") ) <br>result: <s:property value="#parameters['check']" /><h1> 測試2 : #session </h1>( 相當于 session.getAttribute("") ) <br>result: <s:property value="#session['user']" /> <br>另外一種寫法 : <s:property value="#session.user" /><h1> 測試3 : #reqeust</h1>#request['xxx'] 相當于 request.getAttribute("xxx") <br>#parameters['xxx'] 相當于 request.getParameter("xxx") <br>兩者不同<h1> 測試4 :#action </h1>result: <s:property value="#action['reason']" /> <br>相當于: <s:property value="reason" /><h1> 測試5 :if判斷 </h1><s:if test="result > 100" >夠大!</s:if><s:else>太小了!</s:else><h1> 測試6 :if判斷 </h1><s:if test="#session['user'] == null" >未登錄</s:if><s:else><s:property value="#session['user']" />,您好!</s:else></body> </html>




總結

以上是生活随笔為你收集整理的Struts2 访问上下问对象的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。