我在将请求参数从一个servlet转发到另一个servlet时遇到问题

时间:2017-05-01 12:46:53

标签: java servlets

我正在尝试从index.html获取一些参数并调用servlet。然后我添加一些参数来请求并转发到第二个servlet。问题是它没有识别添加的参数并显示为null。

Servlet 1

    package lets.use.packagehere;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.apache.catalina.Context;

import lets.use.packagehere.model.Bean;
@WebServlet(urlPatterns =  "/test")

public class servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("hi");
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub
        PrintWriter p = response.getWriter();

        HttpSession sessionsave = request.getSession();
        ServletContext application=getServletConfig().getServletContext();

        if(!(request.getParameterValues("list")==null))
        {
            sessionsave.setAttribute("value",request.getParameterValues("list")[0] );
            application.setAttribute("value",request.getParameterValues("list")[0] );
        }
        p.println(request.getParameter("name")+ sessionsave.getAttribute("value")+application.getAttribute("value"));                                       

        Bean test = new Bean();
        p.println(test.getA());
        request.setAttribute("check", "hi");
        RequestDispatcher check = request.getRequestDispatcher("S2");
        check.forward(request, response);

    }

}

Servlet 2

package lets.use.packagehere;

import java.io.IOException;
import java.io.PrintWriter;

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

@WebServlet(urlPatterns =  "/S2")
public class CHECK extends HttpServlet {
    private static final long serialVersionUID = 1L;


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

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter p = response.getWriter();

        p.println(request.getParameter("name")+request.getParameter("check") );
    }

}

索引文件

<--nocode-->
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <form method="post" action="servlet">
            Name<input name="name"> 



            <select name="list" multiple="multiple">

                <option value="tech">Technology</option>
                <option value="admin">Administration</option>
                <option value="biology">Biology</option>
                <option value="science">Science</option>


            </select> <br> <input type="submit">

        </form>
    </body>
    </html>

输出

Hinull

我期待着Hihi

1 个答案:

答案 0 :(得分:0)

应在Check类中使用request.getAttribute(“ check”)而不是request.getParameter(“ check”)。