package com.bjsxt.servlet.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServeltRequest
*/
@WebServlet("/ServeltRequest")
public class ServeltRequest extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//请求信息
//请求行(请求方式,资源路径,协议和版本号)
getrequestline(req,resp);
//请求头
//请求实体内容
//获取网络信息
}
private void getrequestline(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("ServeltRequest.getrequestline(获取请求方式):"+req.getMethod());
System.out.println("ServeltRequest.getrequestline(资源路径uri):"+req.getRequestURI());
System.out.println("ServeltRequest.getrequestline(资源路径url):"+req.getRequestURL());
System.out.println("ServeltRequest.getrequestline(协议):"+req.getScheme());
System.out.println("ServeltRequest.getrequestline(版本号)"+req.getProtocol());
System.out.println("ServeltRequest.getrequestline(项目路径)"+req.getContextPath());
System.out.println("ServeltRequest.getrequestline(请求参数)"+req.getQueryString());
}
}
package com.bjsxt.servlet.response;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//HttpServletResponse:包含所有的响应信息,对象由servlet容器创建
public class ServletResponse extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//响应信息格式
//响应行(响应协议和版本 ,状态吗,状态吗描述)-- 响应行servlet自动处理
//响应头
//设置响应头(同名的响应头会被覆盖)
resp.setHeader("iphone", "iphone7");
resp.setHeader("iphone", "iphone8");
//添加响应头(同名的响应头不会被覆盖)
resp.addHeader("vivo", "vivoX5");
resp.addHeader("vivo", "vivoX6");
//设置浏览器的解码格式
//resp.setHeader("content-type", "text/html;charset=utf-8");
//简单写法
resp.setContentType("text/html;charset=utf-8");
//响应的实体内容
PrintWriter pw = resp.getWriter();
pw.print("<html>");
pw.print("<head>");
pw.print("</head>");
pw.print("<body>");
pw.print("<font style='color:red'>");
pw.print("星期天去西单看美女");
pw.print("</font>");
pw.print("</body>");
pw.print("</html>");
}
}