我想創建一個servlet類,它接收來自jsp的兩個輸入參數let say login.jsp和thae servlet「CommandQueueTestServlet」將那些包含參數的參數設置爲頭參數,然後發送請求和響應參數到另一個servlet「CheckForCommandServlet」。手動設置測試頭
我需要這樣做只是爲了測試我的功能,因爲我的「CheckForCommandServlet」實際上會被其他具有頭部參數的應用程序調用。
但是對於我自己的測試,我想創建一個servlet「CommandQueueTestServlet」來設置標題。
請檢查下面的代碼,我試圖解釋
javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class CommandQueueTestServlet extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String hwId=request.getParameter("hardware_id");
String panelistId=request.getParameter("panelist_id"));
// Setting input parameter as header parameter.Since request object dont have setHeader so setting in response
//object
response.setHeader("x-HwId",hwid);
response.setHeader("x-panelistId,panelistId);
// creating instance of CheckForCommandServlet and passing in doGet() method:
CheckForCommandServlet headerParam= new CheckForCommandServlet();
headerParam.doGet(request,response);
}
}
// Code for CheckForCommandServlet
public class CheckForCommandServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Enumeration enumeration = httpServletRequest.getHeaderNames();
String headerName;
String headerValue;
while (enumeration.hasMoreElements())
{
headerName = (String)enumeration.nextElement();
if (headerName == null)
{
headerName = "";
}
headerName = headerName.toLowerCase();
headerValue = httpServletRequest.getHeader(headerName);
logger.log(Level.INFO, "Header headerName " + headerName);
logger.log(Level.INFO, "Header ParamaterValue " + headerValue);
}
}
我CheckForCommandServlet是怎麼得到的headerParemeter在CommandQueueTestServlet設置,因爲它是在頭參數設置。
謝謝大家的優秀回覆! – vibhas