我需要通过使用POST方法传递一些参数来重定向从重定向类到servlet(我需要像GET的Response.seeOther一样):它有可能以任何方式吗? 通过使用调试器并在浏览器中输入正确的URL,执行restlet类中的doSomething方法,然后执行servlet中的doPost方法并正确传递参数,显然没有任何异常,但浏览器没有什么都没有。 我在doSomething方法中尝试的方式是正确的,还是让我尝试另一种方式? 哪个是POST重定向的正确代码(如果可能的话)? restlet类只有以下方法:
@GET
public void doSomething(
@PathParam("request") String req,
@PathParam("lang") String lang,
@PathParam("query") String query) {
try
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost:8080/trypost/mainservlet");
//post.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpParams params = new BasicHttpParams();
params.setParameter("REQUEST", URLEncoder.encode(req, "UTF-8"));
params.setParameter("LANG", URLEncoder.encode(lang, "UTF-8"));
params.setParameter("QUERY", URLEncoder.encode(query, "UTF-8"));
post.setParams(params);
HttpResponse response = httpClient.execute(post);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
servlet中doPost方法的代码如下:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) {
String req = request.getParameter("REQUEST");
String lang = request.getParameter("LANG");
String query = request.getParameter("QUERY");
try {
PrintWriter w = response.getWriter();
w.println("hello world by MainServlet");
w.println(req);
w.println(lang);
w.println(query);
w.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
web.xml的代码如下:
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>trypost.MainServlet</servlet-class>
<init-param>
<param-name>trypost.MainServlet.execOnGET</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>trypost.MainServlet.execOnPOST</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/mainservlet/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>trypost</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.servlet.ServletContainer.execOnGET</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.servlet.ServletContainer.execOnPOST</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>