从JSP向Servlet发送整数

时间:2020-02-01 19:41:55

标签: javascript java jquery html jsp

无法从Servlet向JSP发送整数。

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

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Method Get in MyServlet");
        int Gconta = Integer.parseInt(request.getParameter("Gcont"));
        Gconta = Gconta + 1;
        System.out.println(Gconta);

        HttpSession session = request.getSession();
        session.setAttribute("NGconta", Gconta);
        response.sendRedirect("cabecalhost.jsp");

        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

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

    }
}

JSP页面

<body>  
    <h3>O Resultado NGconta:</h3> 

    <%String Getnumbx = request.getParameter("NGconta"); %>
    <%= Getnumbx %>

    <%String Getnumb = (String) request.getAttribute("NGconta");%>
    <% out.println("Your Result is "+ Getnumb);  %>

    &{NGconta}; 

</body>

2 个答案:

答案 0 :(得分:1)

尝试

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

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Method Get in MyServlet");
        int Gconta = Integer.parseInt(request.getParameter("Gcont"));
        Gconta = Gconta + 1;
        System.out.println(Gconta);

        HttpSession session = request.getSession();
        session.setAttribute("NGconta", Gconta);
        RequestDispatcher rd = 
        request.getRequestDispatcher("cabecalhost.jsp");
        rd.forward(request, response);

    }

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

    }
}


<body>  
    <%String Getnumbx = (String)request.getSession().getAttribute("NGconta"); %>
    <%= Getnumbx %>
</body>

答案 1 :(得分:0)

替换

response.sendRedirect("cabecalhost.jsp");

使用

request.getRequestDispatcher("cabecalhost.jsp").forward(request, response);

以及JSP中的代码

<body>  
    <h3>O Resultado NGconta:</h3> 

    <%String Getnumbx = session.getAttribute("NGconta").toString(); %>
    <%= Getnumbx %>

    <%String Getnumb = session.getAttribute("NGconta").toString();%>
    <% out.println("Your Result is "+ Getnumb);  %>

    ${NGconta}; 

</body>

说明:

  1. redirect指示服务器为目标URL创建新请求,因此,现有request的内容将对目标URL不可用。另一方面,forward将现有request对象转发到目标URL,因此您可以在目标URL上检索现有request的内容。检查类似的post以获得更好的理解。
  2. $(而不是&)用作JSP表达式语言运算符,以从不同的范围变量中检索值。