Servlet核心接口和类
创建servlet,除了实现Servlet接口之外,还可以继承 GenericServlet 或 HttpServlet。
HttpServlet类
继承HttpServlet类创建Servlet
package com.ws.servlet_;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HttpsServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get请求");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post请求");
}
}
Servlet配置方法
注解方式配置
package com.ws.Servlet2;
import com.ws.servlet_.HttpsServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value = "/hello_http")//注解方式配置,value的值为请求的路径
public class BasicServlet extends HttpsServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("GET");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("POST");
}
}
requests对象
servlet中,get和post请求的数据都在HttpServletRequest对象中,两个核心接口是 ServletRequest、HttpServletRequ
方法
方法名 | 说明 |
---|---|
String getParameter(String name) | 根据表单组件名称获取提交的数据 |
void setCharacterEncoding(String charset) | 指定每个请求的编码 |
get和post请求
通过get和post请求来获取注册页面的数据。
get方式
package com.ws.Servlet2;
import com.ws.servlet_.HttpsServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value = "/register")
public class RegisterServlet extends HttpsServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取用户请求发送的数据
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println("提交的数据"+username+"\t"+password);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="/servlet_war_exploded/register" method="get">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>
post方式
package com.ws.Servlet2;
import com.ws.servlet_.HttpsServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value = "/register")
public class RegisterServlet extends HttpsServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//对request请求对象设置统一的编码,中文服务器端接收会乱码
req.setCharacterEncoding("utf-8");
//获取用户请求发送的数据
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println("提交的数据"+username+"\t"+password);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="/servlet_war_exploded/register" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>
response对象
response对象用于响应客户端请求病向客户端输出信息。
response主要方法
方法名称 | 作用 |
---|---|
setHeader(name,value) | 设置响应信息头 |
setContentType(String) | 设置响应文件类型、响应式的编码格式 |
setCharacterEncoding(String) | 设置服务端响应内容编码格式 |
getWriter() | 获取字符输出流 |
package com.ws.Servlet2;
import com.ws.servlet_.HttpsServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(value = "/register")
public class RegisterServlet extends HttpsServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//对request请求对象设置统一的编码
req.setCharacterEncoding("utf-8");
//获取用户请求发送的数据
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println("提交的数据"+username+"\t"+password);
// //设置服务器端响应的编码格式
// resp.setCharacterEncoding("utf-8");
// //设置客户端响应内容的头内容的文件类型及编码格式
// resp.setHeader("Content-type","text/html;charset=UTF-8");
//同时设置服务器端的响应格式和客户端的头内容编码
resp.setContentType("text/html;charset=UTF-8");
//响应数据给客户端
PrintWriter writer = resp.getWriter();
writer.println("登录成功!");
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="/servlet_war_exploded/register" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>