当用户输入url的时候,他的访问步骤依次如下
Servlet标准 API核心包
Servlet API有以下3个Java包:
代码示例
1.首先在webapp下创建一个静态文件requestTest.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--创建一个form表单,用于给servlet发送请求-->
<!--请求动作可以是get也可以是post,然后我们这里请求到request,那么我们就需要创建一个request路由-->
<form action="Get" action="request">
<input name="uname" placeholder="请输入用户名">
<input type="submit" value=" 提 交 ">
</form>
</body>
</html>
2.写动态的servlet,那么就需要在两个地方进行写入
2.1首先写后端类requestServlet
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
//必须继承HttpServlet
public class requestServlet extends HttpServlet {
//重写doGet()方法和doPost()方法
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//拿到参数的值
String uname = req.getParameter("uname");
//拿到request的方法
String methed = req.getMethod();
//得到url
String url = req.getContextPath();
//获取协议的名称
String pocl = req.getProtocol();
//获取编码
String encoding = req.getCharacterEncoding();
//然后把这些内容写回给客户端,首先设置编码格式和类型,然后再是返回的内容
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println(String.format("请求参数: %s
",uname));
writer.println(String.format("method: %s
",methed));
writer.println(String.format("url: %s
",url));
writer.println(String.format("协议名称pocl: %s
",pocl));
writer.println(String.format("编码encoding: %s
",encoding));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//我们让doPost方法直接调用doGet方法
this.doGet(req,resp);
}
}
2.2 在web.xml配置一组路由信息
<servlet>
<servlet-name>reqServlet</servlet-name>
<servlet-class>requestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>reqServlet</servlet-name>
<url-pattern>/request</url-pattern>
</servlet-mapping>
代码案例:
如拿到当前的时间,然后每隔一秒进行一次刷新操作
步骤:
1.创建一个servlet后端类
2.在web.xml配置一组路由信息
后端类代码
import javax.servlet.ServletException;
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.Date;
public class MyResponseServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//刷新操作,设置成1s刷新一次
resp.setIntHeader("Refresh",1);
//设置响应类型
resp.setContentType("text/html");
//设置编码格式,如果不设置会出现乱码
resp.setCharacterEncoding("utf-8");
//写入body信息
PrintWriter writer = resp.getWriter();
writer.println(String.format("当前时间: %s
",new Date()));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
配置web.xml
<servlet>
<servlet-name>myResponseServlet</servlet-name>
<servlet-class>MyResponseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myResponseServlet</servlet-name>
<url-pattern>/response</url-pattern>
</servlet-mapping>
在当我们访问一个页面的时候,输入的url是无效的就会出现404错误,比如最简单的就是出现一个
比如上图中右边的404页面如何显示在我们的servlet中呢?
步骤:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class NotFountServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setStatus(404);
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter writer = resp.getWriter();
writer.println("");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
3.配置web.xml文件
<servlet>
<servlet-name>notFountServlet</servlet-name>
<servlet-class>NotFountServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>notFountServlet</servlet-name>
<url-pattern>/404</url-pattern>
</servlet-mapping>