页面跳转之session

题意:设主页为index.jsp 通过session将index.jsp中的信息传送给结果页面result.jsp。(其实,session的生命周期是整个服务器开启过程,具体不做详细介绍)。这个 其实 挺简单的,就几行代码的事情。希望能帮助迷茫着的人。(感觉自己走出了迷茫了,嘿嘿)。

第一步:写index.jsp页面。设置好要传值的name。

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form method = "post"  action = "login">
    用户名:<input type = "text"  name = "name" >
    密码:<input type = "password"  name = "password">
    <input type ="submit" value = "提交">
</form>
</body>
</html>

第二部:strut2.xml配置(action配置)在src目录下面创建(这样才能被服务器找到)

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="WEB-INF" extends="struts-default">
            <action name="login" class="com.session.test.Login">
                <result name="success">Result.jsp</result>
            </action>
    </package>
</struts>

第三部(两种方法)

  3.1.1编写Login类,用session方法获取name值,用于整个服务器开启周期内。

package com.session.test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class login extends ActionSupport{
    private String name;
    private String password;
    
      public String execute() throws Exception {
              ActionContext context=ActionContext.getContext();
            context.getSession().put("user",name);
            context.getSession().put("password",password);
            return SUCCESS;
      }
}

  3.1.2再编写result.jsp页面。用String name = (String) session.getAttribute("name");获取

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% 
    String name = (String) session.getAttribute("name");
    String password = (String) session.getAttribute("password");
%>
用户名:<%= name %>
密码:<%= password %>
</body>
</html>

3.2.1

不用配置strut2.xml文件,直接从页面中获取值。为了更好的体现,设置了一个中间页面,midContinue.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% 
    String name = request.getParameter("name");
    session.setAttribute("name",name);
    String password = request.getParameter("password");
    session.setAttribute("password",password);
%>
<a href="Result.jsp">继续</a>
</body>
</html>

3.1.3用session传值

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<% 
    String name = (String) session.getAttribute("name");
    String password = (String) session.getAttribute("password");
%>
用户名:<%= name %>
密码:<%= password %>
</body>
</html>

好了这是整个过程,两种方式,希望能帮助需要的人,也不枉我又是敲代码,又是自己敲字。

收工!有问题请回复,这是我自己写的第一篇,期望提些意见。

posted @ 2013-09-07 18:17  红萝卜头  阅读(2190)  评论(0编辑  收藏  举报