Web容器是JSP/Servlet唯一认识的HTTP服务器。
HTTP是基于请求/响应的无状态通信协议。
流程:
1、请求来到HTTP服务器
2、HTTP服务器将请求转交给Web容器
3、Web容器创建代表当前请求的HttpServletRequest对象与对客户端进行响应的HttpServletResponse
4、容器根据读取@WebServlet或者web.xml找出处理本次请求的Servlet并调用它的service()方法,HttpServletRequest与HttpServletResponse作为参数。service()方法根据HTTP请求的方式调用对应的doXXX()方法。
5、在doXXX()方法中可以调用HttpServletRequest与HttpServletResponse对象的相关方法。
6、最后由Web容器将输出转化为HTTP响应,再由HTTP服务器对浏览器响应。之后容器将HttpServletRequest与HttpServletResponse销毁回收,该次请求响应结束。
HTML范例
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 java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
/**
* Created by N3verL4nd on 2017/1/4.
*/
@WebServlet("/hello.do")
public class HelloServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("post ");
out.println("");
out.println("");
//方法1
request.setCharacterEncoding("UTF-8");
//取得请求参数
String usr = request.getParameter("usr");
String pwd = request.getParameter("pwd");
//方法2
//usr = new String(usr.getBytes("ISO-8859-1"), "UTF-8");
//psd = new String(psd.getBytes("ISO-8859-1"), "UTF-8");
//输出字符编码信息
//out.println(request.getCharacterEncoding());
out.println("账号:" + usr + "
密码:" + pwd + "
");
out.println("");
out.println("");
out.close();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置响应内容格式
response.setContentType("text/html;charset=utf-8");
//取得输出对象
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("get ");
out.println("");
out.println("");
//1、取得请求参数(Tomcat --> server.xml --> useBodyEncodingForURI="true")
request.setCharacterEncoding("UTF-8");
//String usr = request.getParameter("usr");
//String pwd = request.getParameter("pwd");
//2、Tomcat容器默认编码为:ISO-8859-1
//usr = new String(usr.getBytes("ISO-8859-1"), "UTF-8");
//psd = new String(psd.getBytes("ISO-8859-1"), "UTF-8");
//out.println("账号:" + usr + "
密码:" + pwd + "
");
Enumeration enumeration = request.getParameterNames();
while (enumeration.hasMoreElements()){
String element = enumeration.nextElement();
if (element.equals("like")){
String[] values = request.getParameterValues(element);
out.println(element + ":");
for (String str : values){
out.println(str + ";");
}
out.print("
");
}
else{
out.println(element + ":" + request.getParameter(element) + "
");
}
}
out.println("");
out.println("");
out.close();
}
}
在Web应用程序中,经常需要多个Servlet来完成请求。
1、将另一个Servlet的请求处理流程包含进来。
2、将请求转发给别的Servlet来处理,这个动作在Web容器中进行,浏览器并不知道请求被转发。
//传送服务器默认的状态和错误 //response.sendError(HttpServletResponse.SC_NOT_FOUND); //要求浏览器重新请求另一个URL response.sendRedirect("https://www.baidu.com");Include,Forward,sendRedirct的区别
http://blog.csdn.net/kkdelta/article/details/4129458
http://blog.csdn.net/evankaka/article/details/45169569
getWriter和getOutputStream 二者只能选其一,调用了其中一个就不能调用另一个了
20-Feb-2017 14:12:14.278 严重 [http-nio-8080-exec-6] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Some] in context with path [/jspRun] threw exception
java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:576)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:194)
request.getRequestDispatcher("WEB-INF/web.xml").forward(request, response);getOutputStream方法用于返回Servlet引擎创建的 字节输出流对象,Servlet程序可以按字节形式输出响应正文。