struts2在Action中访问Session,管理员删除用户
发布日期:2021-06-30 10:51:57 浏览次数:2 分类:技术文章

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

结构如下:

当没有登录admin用户去删除时:

当登录admin用户删除时:

首先是一个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 }}

UserDeleteAction.java

package my;import java.util.Map;import org.apache.log4j.Logger;import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionSupport;public class UserDeleteAction extends ActionSupport	implements SessionAware{		static Logger logger=Logger.getLogger(UserDeleteAction.class);	int id=-1;	//要删除的id	String reason;	//结果			private Map
userSession; @Override public void setSession(Map
arg0) { userSession=arg0; } @Override public String execute() throws Exception { String user=(String)userSession.get("user"); if(!"admin".equals(user)){ reason="无权限进行删除!"; return "error"; } if(id<=0){ reason="请输入删除的ID!"; return "error"; } return "success"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } }

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    	
用户名:
密码:

UserDelete_error.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 'UserDelete_error.jsp' starting page    	

操作失败

UserDelete_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 'UserDelete_success.jsp' starting page    	
操作成功。用户 [
] 被删除!

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

上一篇:Java题-直接赋值与重新创建内存
下一篇:Struts2 访问上下问对象

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月15日 00时50分00秒