上述的代码如下:
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.util.Enumeration;
@WebServlet("/request")
public class RequestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 下面这个操作也是必要的. 显式告诉浏览器, 你拿到的数据是 html.
resp.setContentType("text/html");
StringBuilder respBody=new StringBuilder();
respBody.append(req.getProtocol());
//使用 <br> 标签表示换行
respBody.append("<br>");
respBody.append(req.getMethod());
respBody.append("<br>");
respBody.append(req.getRequestURI());
respBody.append("<br>");
respBody.append(req.getContextPath());
respBody.append("<br>");
respBody.append(req.getQueryString());
respBody.append("<br>");
//Enumeration 枚举类型,每一个枚举类型都是一个键值对,即headers中包含多个键值对
Enumeration<String> headers=req.getHeaderNames();
while(headers.hasMoreElements()){//一开始的时候,并不指向第一个键值对的name
String header= headers.nextElement();//指向第一个键值对的name
respBody.append(header + ": "+req.getHeader(header));//getHeader:获取键值对的值
respBody.append("<br>");
}
resp.getWriter().write(respBody.toString());
}
}
上述的代码如下:
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;
@WebServlet("/parameter1")
public class ParameterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 约定, 客户端使用 query string 传递数据.
// query string 形如: username=zhangsan&password=123
String user_name=req.getParameter("username");
String pass_word=req.getParameter("password");
System.out.println("username"+":"+user_name);
System.out.println("password"+":"+pass_word);
resp.getWriter().write("ok");
}
}
上述的代码如下:
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;
@WebServlet("/parameter2")
public class ParameterServlet2 extends HttpServlet {
// 预期让客户端发送一个 POST 请求. 同时使用 form 格式的数据, 在 body 中把数据传递过来.
// body 形如:
// username=zhangsan&password=123
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String user_name=req.getParameter("username");
String pass_word=req.getParameter("password");
System.out.println("username :"+ user_name);
System.out.println("password :"+ pass_word);
resp.getWriter().write("okk");
}
}
上述过程的代码如下:
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;
@WebServlet("/status")
public class StatusServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//resp.setStatus(404);
resp.setContentType("text/html;charset=utf8");
resp.sendError(500);
resp.getWriter().write("okkkk");
}
}
上述过程的代码如下:
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;
@WebServlet("/refresh")
public class RefreshServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//每2秒刷新一次
resp.setHeader("refresh","2");
resp.getWriter().write("time "+ System.currentTimeMillis());
}
}
上述过程的代码如下:
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;
@WebServlet("/redirect")
public class RedirectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//resp.setStatus(302);
//resp.setHeader("Location","http://www.baidu.com");
resp.sendRedirect("http://www.baidu.com");
}
}
上述过程的代码如下:
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;
@WebServlet("/body")
public class BodyServlet extends HttpServlet {
@Override
//让服务器返回一个html的数据
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf8");
resp.getWriter().write("<div>你们好呀<div>");
}
}