servlet_5

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



/*请求报文的结构:
* 【1】:请求首行
* 【2】:请求头
* 【3】:空行
* 【4】:请求体
*
* HttpServletRequest
* 代表:浏览器发送给服务器的请求报文
* 获取:该对象由Tommat服务器创建,最终作为参数传递到doGet或doPost方法中
* 功能:
* 【1】:获取请求参数
* 【2】:可以获取项目名
* 【3】:可以作为一个域对象在不同的web资源之间共享数据(request servletContext)
* 【4】:可以做请求的转发
*
*
*
*
*
*
*/
//获取请求参数
/*String username = request.getParameter("username");
String password = request.getParameter("password");
//在页面中输出用户名
response.getWriter().print("<h1>username="+username+"</h1>");
System.out.println(password);
//获取项目名
//主要用来获取绝对路径
String contextPath = request.getContextPath();
System.out.println(contextPath);

/*请求的转发
*
* 所谓的转发就是指收到浏览器发送的请求以后
* 当前servlet不去处理请求
* 而是去调用服务器内部的资源去处理请求
* 转发的特点:
* 【1】转发时浏览器发送了1次请求
* 【2】转发是在服务器内部进行的
* 【3】浏览器的地址栏不会发生改变
* 【4】浏览器不知道转发的发生
*
*
*/
//1.获取一个请求的派发器*/

RequestDispatcher dispatcher = request.getRequestDispatcher("target.html");
dispatcher.forward(request, response);

}

原文地址:https://www.cnblogs.com/fanzhengzheng/p/7572149.html