日期:2014-05-17  浏览次数:20798 次

小白问个JSP与servlet的传参问题
JSP页面代码如下
Java code
<%@ page import="java.sql.*" %>
<%@ page import="java.lang.*" %>
<%@page contentType="text/html" pageEncoding="GB2312"%>
<!DOCTYPE html>
<BODY>
    <P>输入姓名:
    <FORM action="Count" method="get">
        <INPUT type=text name=number>
        <INPUT type=submit value="提交">
        <%  
            String rea1 = request.getPatameter("read");
            out.print(read1);
        %>
    </FORM>
</body>

servlet代码如下
Java code

public class Count extends HttpServlet {
    public void service(HttpServletRequest req,HttpServletResponse res)throws IOException
    {   
        String read = "darc";
        PrintWriter out = res.getWriter();
        res.setContentType("text/html;charset=GB2312");
        String number = req.getParameter("number");
        req.setCharacterEncoding("gbk");
        out.print("<BR>"+"hello"+number);
    }
}


目的就是想让servlet中定义的String read参数传到JSP页面中显示出来,JSP页面打印出来的read1是null,请问代码有什么问题

------解决方案--------------------
request.setAttubute("read",read);
request.getRequestDispatcher("/url").forward(request,response);
------解决方案--------------------
servlet里把read放到request里,在页面用你的方法取就好,你现在只是取,但还没有放进去,所以是空值
------解决方案--------------------
Servlet要这样写才能跳转向jsp文件,你的写法两个文件根本无关
request.setAttubute("read",read); 
//假设你的jsp文件名称是:test.jsp
request.getRequestDispatcher("test.jsp").forward(request,response);
------解决方案--------------------
request.setAttubute("read",read); 存到作用域里面去了
 JSP才能获得这个read的值