回 帖 发 新 帖 刷新版面

主题:servlet调用javabean的问题。

我想在servlet中用javabean的setter方法把数据写到javabean中,然后跳转到另一个
jsp页面,此jsp页面从javabean中把刚才serlvet写进去的数据getter出来。
可是问题是:在jsp中getter不到刚才servlet写入的数据,而是空值。
请问如何解决??注:必须要在servlet中把数据写入到javabean中

回复列表 (共1个回复)

沙发

page1:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
                String path = request
                .getContextPath();
            String basePath = request
                .getScheme() + "://" + request
                .getServerName() + ":" + request
                .getServerPort() + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">

        <title>My JSP 'page1.jsp' starting page</title>

        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

    </head>

    <body>
        This is my JSP page.
        <br>
        <form action="sends" method="post">
            <input type="text" name="param"/>
            <input type="submit" name="submit" value="commit"/>
        </form>
    </body>
</html>
page2:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'page2.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <%=request.getParameter("param")%>
  </body>
</html>
servlet:
package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import form.ParamForm;


public class MyServlet
        extends
            HttpServlet {

    /**
     * --
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(
            HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException,
                IOException {

        // TODO Auto-generated method stub
        super
            .doGet(
                request,
                response);
    }

    @Override
    protected void doPost(
            HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException,
                IOException {

        ParamForm param=new ParamForm();
        param.setParam(request.getParameter("param"));
        request.setAttribute("param", param.getParam());
        request.getRequestDispatcher("page2.jsp").forward(request, response);
    }

}
Form:
package form;


public class ParamForm {

    private String param;

    
    public ParamForm() {
        
        param="";
    }

    

    public String getParam() {
    
        return param;
    }

    
    public void setParam(
            String param) {
    
        System.out.println(save(param)?"the date is saved!":"the date is not saved!");
        this.param = param;
        
    }


    @Override
    public String toString() {
        
        return "[param:"+param+"]";
    }
    
    public boolean save(String data){
        
        //数据库处理操作
        return true;
    }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册