常见的状态码
-
200
服务器正常响应
-
302
页面的跳转
-
304
表示使用缓存 -
404
地址栏输入错误,注意区分大小写
-
405
.没有doGet或doPost处理方法
-
500
服务器内部错误(代码出错或者导报错误)
页面跳转得使用案例
登陆页面代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<h2>用户登录</h2>
<form action="demo2" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</body>
</html>
页面
注意:登陆页面跳转得地址是demo2
效果图
demo2代码
package com.zjs.servlet;
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;
@WebServlet("/demo2")
public class Demo02Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置刷新 3秒后跳转到demo1
response.setHeader("refresh","3;url=demo1");
//设置字符集
response.setContentType("text/html;charset=utf-8");
//得到打印流
PrintWriter out = response.getWriter();
//打印3秒后跳转
out.print("3秒后跳转到");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
demo2 跳转到demo1
demo1代码
package com.zjs.servlet;
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;
@WebServlet("/demo1")
public class Demo01Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
response.setContentType("text/html;charset=utf-8");
//得到打印流
PrintWriter out = response.getWriter();
out.print("大哥哥");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}