响应客户端请求(servlet与jsp)

时间:2022-10-17 21:02:58

同时处理客户端的get请求和post请求:

Servlet:

public class getAndPost extends HttpServlet
{	
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException , IOException
	{
		response.setContentType("text/html");
		//获取参数
		request.getParameter("type");
		
		//获取字符输出流,返回参数值
		PrintWriter writer = response.getWriter();
		writer.print(ret);
		writer.flush();
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//doGet调用doPost,实现Get请求
		doPost(req, resp);
	}
}

web.xml配置:

	<servlet>
		<servlet-name>market</servlet-name>
		<servlet-class>com.test.MarketService</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>market</servlet-name>
		<url-pattern>/market</url-pattern>
	</servlet-mapping>

访问:http://localhost:8080/server/market?type=5

JSP: test.jsp

<%
String type = request.getParameter("type");
out.println("type=="+type);
out.println("OK");
%>

访问:
http://localhost:8080/server/test.jsp?type=5  返回type=5 OK