session对象使用示例
携带用户名往下跳转的例子
首先把我们需要实现的功能用到的2 个页面建立好 。login.jsp 和success.jsp
login.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title></head><body><form id="form1" name="form1" method="post" action="login1.do"> 用户名 <label> <input name="username" type="text" id="username" /> </label> <p>密码 <label> <input name="password" type="text" id="password" /> </label> </p> <p> <label> <input type="submit" name="Submit" value="提交" /> </label> </p></form></body></html>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title></head><body>${name} 登陆成功</body></html>
/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */package form;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionMapping;public class LoginForm extends ActionForm {/* * Generated fields *//** password property */private String password;/** username property */private String username;public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {// TODO Auto-generated method stubreturn null;}public void reset(ActionMapping mapping, HttpServletRequest request) {// TODO Auto-generated method stub}/** * Returns the password. * @return String */public String getPassword() {return password;}/** * Set the password. * @param password The password to set */public void setPassword(String password) {this.password = password;}/** * Returns the username. * @return String */public String getUsername() {return username;}/** * Set the username. * @param username The username to set */public void setUsername(String username) {this.username = username;}}
package 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 form.LoginForm;public class Login1Action extends Action {/** * 这里面要用到session */public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stubHttpSession bb=request.getSession();//建立个session对象String username =loginForm.getUsername();String password =loginForm.getPassword();if (username.equals("w")&&password.equals("w")){bb.setAttribute("name",username);//用到的是setAttribute方法return mapping.findForward("ok");}return mapping.findForward("nook");}}