struts如何在类文件class和action中读写session? 比如在login.java中将登录用户信息写入session,在action中每次都先取出用户信息以判断权限?使用session需要import哪些包?最好给个例子,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java的api里面都有啊你用的时候自动导入就可以package com.hy.struts.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import com.hy.struts.form.CsdnForm;public class CsdnAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { CsdnForm csdnForm = (CsdnForm) form;// TODO Auto-generated method stub HttpSession hs = request.getSession(); hs.getAttribute(""); return null; }}其他的信息你自己搞定就可以了啊 //导入这个包import javax.servlet.http.HttpSession;HttpSession session = request.getSession(); 然后像JSP里面一样直接用session就好了,当然也可以换成别的名字 一般不在Action中做这件事,这样太麻烦了,万一有一百个Action那得重复写一百遍的。可以配置一个Filter,在Filter中做这件事。 做一个Filter,在web.xml中配置一下。Filter中的doFilter方法大致如下:public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest)request; HttpServletResponse httpResponse = (HttpServletResponse) response; HttpSession session = httpRequest.getSession(); if(session.getAttribute(ApplicationSource.USER) == null && !httpRequest.getRequestURI().endsWith("/login.jsp") // 这些都是一些需要排除的页面,避免出现死循环 && !httpRequest.getRequestURI().endsWith("/calendar/") && (httpRequest.getRequestURI().indexOf("/calendar/img") < 0) && (httpRequest.getRequestURI().indexOf("/calendar/resources") < 0) && !httpRequest.getRequestURI().endsWith("/login.do")){ // 除了以上的这些页面和资源外,对于其他的页面,凡session中没有用户存在,就转到登录页面 httpResponse.sendRedirect(httpRequest.getContextPath() + "/login.jsp"); } // 否则的话将请求发送给接收方,过滤器好比是页面与Action中的一层滤网 chain.doFilter(request, response);} Servlet测试不成功! 多线程中 try catch 问题 来个积分下载东西,我哭啊 java如何读写pdf文件,并转化成二进制文件。 List的排序 高手帮忙解答关于组件监听的问题?小弟先谢了! 数据库查询中一个非常奇怪的问题,你遇到过吗? 通过jdbc-odbc访问SqlServer使用prepareStatement时,xxxx like %?%……出现问题,谁知道是怎么回事?如何解决? Cappuccino : Memory Leak & SoftReference swing编程问题 这段代码有问题吗? 如果没问题 输出是多少? jdbc连接sql
你用的时候自动导入就可以
package com.hy.struts.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import com.hy.struts.form.CsdnForm;public class CsdnAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
CsdnForm csdnForm = (CsdnForm) form;// TODO Auto-generated method stub
HttpSession hs = request.getSession();
hs.getAttribute(""); return null; }
}其他的信息你自己搞定就可以了啊
import javax.servlet.http.HttpSession;HttpSession session = request.getSession();
然后像JSP里面一样直接用session就好了,当然也可以换成别的名字
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
HttpSession session = httpRequest.getSession();
if(session.getAttribute(ApplicationSource.USER) == null
&& !httpRequest.getRequestURI().endsWith("/login.jsp") // 这些都是一些需要排除的页面,避免出现死循环
&& !httpRequest.getRequestURI().endsWith("/calendar/")
&& (httpRequest.getRequestURI().indexOf("/calendar/img") < 0)
&& (httpRequest.getRequestURI().indexOf("/calendar/resources") < 0)
&& !httpRequest.getRequestURI().endsWith("/login.do")){
// 除了以上的这些页面和资源外,对于其他的页面,凡session中没有用户存在,就转到登录页面
httpResponse.sendRedirect(httpRequest.getContextPath() + "/login.jsp");
}
// 否则的话将请求发送给接收方,过滤器好比是页面与Action中的一层滤网
chain.doFilter(request, response);
}