携带用户名往下跳转的例子
首先把我们需要实现的功能用到的2 个页面建立好 。login.jsp 和success.jsp
login.jsp
success.jsp
注意 ${name} 这里。这里边使用的就是EL表达式。目的就是为了把你在login.jsp中输入的用户名接过来显示在success.jsp中。${name}对应的是bb.setAttribute("name",username)
中的"name",而username是从jsp中的username 属性中得到的。
1 个form
1 个 action
首先把我们需要实现的功能用到的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");
}
}