在jsp页面和servlet之间传递参数

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

标签: java html web-services jsp servlets

我是网络服务jsps和servlets的新手,我有一个非常简单的例子,只是为了理解工作原理。

首先,我有这个简单的网络服务:

    package com.sav.calculator;

    import javax.jws.WebService;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;

    @WebService(serviceName = "CalculatorWS")
    public class CalculatorWS {

        @WebMethod(operationName = "add")
        public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
            int k = i + j;
            return k;
        }

    }

然后我在我的客户端应用程序中使用此Web服务。我试图以正确的方式工作,所以我将数据从jsp发送到servlet,在servlet中进行计算,并在另一个jsp中为演示文稿发送数据..但问题是为什么我没有把它弄好?

这是第一个jsp(只是一个html表单):

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
        <body>

            <form method="POST" action="ClientServlet">
                <input type="text" name="j"/>
                <input type="text" name="i"/>
                <input type="submit" value="submit"/>
            </form>

        </body>
    </html>

这是我使用我的添加webmethod的servlet:

package com.sav.calculator.client;

import com.sav.calculator.CalculatorWS_Service;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
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.xml.ws.WebServiceRef;

@WebServlet(name = "ClientServlet", urlPatterns = {"/ClientServlet"})
public class ClientServlet extends HttpServlet {

    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/CalculatorWSApplication/CalculatorWS.wsdl")
    private CalculatorWS_Service service;

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

    }

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

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

        int i = (int) request.getAttribute("i");
        int j = (int) request.getAttribute("j");

        int k = add(i, j);
        request.setAttribute("k",k);

        RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("newjsp2.jsp");
        dispatcher.forward(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

    private int add(int i, int j) {
        com.sav.calculator.CalculatorWS port = service.getCalculatorWSPort();
        return port.add(i, j);
    }

}

而newjsp2只是一个hello world page,我只是想先到达那里,但我得到的是: that.

2 个答案:

答案 0 :(得分:0)

启动Web服务服务器后,在Web浏览器中键入地址:

 http://localhost:8080/CalculatorWSApplication/CalculatorWS.wsdl

如果此地址包含wsdl(xml格式),则将其用作 wsdlLocation

尝试使用某些工具,例如SoapUI, or some other

答案 1 :(得分:0)

从Servlet到JSP

您可以在将请求转发到jsp之前将值设置到响应对象中。或者,您可以将值放入会话bean并在jsp中访问它。

从JSP到Servlet

您需要提交表单并传递参数作为输入。 示例 ...

<form method="Post" action="path/to/servlet">
    <input type="text" name="x" />
    <input type="password" name="xx" />
    <input type="hidden" name="xxx" value="zzz" />
    <input type='submit' />
</form>