在jsp中,内通过内置对象 HttpServletRequest的getSession()方法可以获取到HttpSession,比如:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://www.wyl.suneyaee" prefix="Wyl"%>
<!-- 引入jquery等 -->
<%@ include file="/WEB-INF/head.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
<!--
$(function() {
$('input').keydown(function(e) {
var curKey = e.which;
//46 是 Delete键
if (curKey == 46) {
clear(this);
$(this).focus();
}
}); $('#idUserName').keydown(function(event) {
var curKey = event.which;
//13 是回车键
if (curKey == 13) {
console.log('你按的是:回车' + '键');
} });
/**
清除value值,主要是针对于input
*/
function clear(obj) {
$(obj).attr('value', null);
}
})
</script> <title>Insert title here</title>
</head>
<body>
<Wyl:getIP /> <form action="doAjax"><input value="aaa" type="text"> <br>
<input value="doAJax" type="submit"></form> <br>
<form action="wdAjax"><input value="MyAjax.myAjax()..."
type="text"> <br>
<input value="wdAjax" type="submit"></form> <br>
用户名
<form action="toLogin"><input type="text" name="user.userName">
<br>
<%
request.getSession().setAttribute("hahaAnyKey", "我是hahaAnyKey.jsp页面传入session的值");
%>
<input type="text" name="user.passWord"> <input type="submit">
</form> <br>
用户名2 ,模型驱动,而且这个表单对应的LoginAction2.java中封装了dto,而且可以使用Delete键清空输入框
<form action="toLogin2"><input type="text" name="userName"
id="idUserName"> <br>
<input type="text" name="passWord"> <input type="submit">
</form> -----------
<span xmlns="http://www.w3.org/1999/xhtml"
class="button collapse-button"></span> </body>
</html>
中的58行,就可以获取到session,然后直接在HttpSession里存放一个值。
而在Struts2中,比如在一个action中,可以直接通过ActionContext.getContext().getSession()获取Session对象(注意:这个ActionContext是
com.opensymphony.xwork2.ActionContext类型的),实际上这里的Session是一个Mapper,只是Struts2做了封装,我们还是可以通过这个mapper类型的Mapper来获取到上面登录页面放在HttpSession中的属性。
比如在一个action中就可以通过如下代码获取到上面jsp中存放在HttpSession中的属性,
ActionContext atx = ActionContext.getContext();
Map<String, Object> session = atx.getSession();
Set<String> set = session.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
String key = (String) it.next();
String val = (String) session.get(key);
System.out.println("key:"+key+",value:"+val);
}
打印结果:
key:hahaAnyKey,value:我是hahaAnyKey.jsp页面传入session的值
这个Action的完整代码如下:
package com.log; import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.log.entity.User;
import com.log.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.UtilTime; /**
* 普通的登陆
*
* @author Wei
*
*/
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
private UserService uService = new UserService();
private String error; public String getEror() {
return error;
} public void setEror(String error) {
this.error = error;
} public UserService getuService() {
return uService;
} public void setuService(UserService uService) {
this.uService = uService;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} @Override
public String execute() throws Exception {
/**
* 账号密码:wyl,1234
*/
if (uService.login(user)) {
ActionContext atx = ActionContext.getContext();
Map<String, Object> session = atx.getSession();
//对应于 WylInterceptor.java拦截器,这个拦截器里需要用到
session.put("currentUser", user);
//用来设置登录超时的参数
session.put("lastTime", UtilTime.getCurTime());
return SUCCESS;
} else {
// this.error = "error";
// return this.error;
HttpServletResponse resp = ServletActionContext.getResponse();
//不加 resp.setContentType("text/html;charset=UTF-8"); 会出现页面的中文为乱码的情况
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.println("账号:"+user.getUserName()+",<br/>密码:"+user.getPassWord()+" 的不正确,请确认");
// out.println("<br/><input type=\"button\" value=\"点我\" />");
out.println("<a href=\"login.jsp\" style=\"color:orange;\">点我重新登陆</a>");
/**
* 下面的Map<String, Object> session = atx.getSession();为了演示获取到的session(实际上是Mapper,Struts2框架做了封装)
*/
ActionContext atx = ActionContext.getContext();
Map<String, Object> session = atx.getSession();
Set<String> set = session.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
String key = (String) it.next();
String val = (String) session.get(key);
System.out.println("key:"+key+",value:"+val);
}
//什么都不返回,直接用HttpServletResponse在网页上写提示
return null;
}
}
}
,调试截图: