session对象使用示例

本文介绍了一个基于Struts框架的简单登录案例,包括login.jsp和success.jsp两个页面的设计。通过表单提交用户名和密码,利用EL表达式在success.jsp页面显示登录成功的用户名。
携带用户名往下跳转的例子

首先把我们需要实现的功能用到的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>

success.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>
${name} 登陆成功
</body>

</html>


注意 ${name} 这里。这里边使用的就是EL表达式。目的就是为了把你在login.jsp中输入的用户名接过来显示在success.jsp中。${name}对应的是bb.setAttribute("name",username)
中的"name",而username是从jsp中的username 属性中得到的。


1 个form

/*
* 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 stub
return 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;
}
}



1 个 action

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 stub
HttpSession 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");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值