我是web服務jsps和servlets的新手,爲了理解工作原理,我有一個非常簡單的例子。在jsp頁面和servlets之間傳遞參數
起初,我有這個簡單的Web服務:
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頁面,即時通訊只是試圖讓那裏第一次但我得到的是: that.