Servlet

1.使用注解方式配置

这种方法不与web.xml冲突。

@WebServlet()注解常用解释:

Servlet - 图1

例:

  1. package com.example.webprojet;
  2. import com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt.GetOpt;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. /**
  10. * @author Zhtao
  11. * @date 2022/2/25 19:35
  12. */
  13. @WebServlet(value = {"/bs","/bss"})
  14. public class BasicServlet extends HttpServlet {
  15. @Override
  16. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  17. System.out.println("这是get");
  18. }
  19. @Override
  20. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  21. System.out.println("这是post");
  22. }
  23. }

此时访问/bs和/bss都可以。

2.request对象

1.request主要方法

方法名 说明
String getParameter(String name) 根据表单组件名称获取提交数据
void setCharacterEncoding(String charset) 指定每个请求的编码

2.request应用

1.Get()请求

  1. package com.qf.servlet2;
  2. import com.sun.javaws.HtmlOptions;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.security.SecureRandom;
  10. /**
  11. * @author Zhtao
  12. * @date 2022/2/25 19:56
  13. */
  14. @WebServlet(value = "/rs")
  15. public class RegisterServlet extends HttpServlet {
  16. // 1.获取用户请求发送的数据
  17. @Override
  18. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  19. String username = req.getParameter("username");
  20. String password = req.getParameter("password");
  21. System.out.println("用户提交的数据:" + username + "\t" + password);
  22. }
  23. }

Servlet - 图2

Servlet - 图3

Servlet - 图4

2.Post()请求

  1. package com.qf.servlet2;
  2. import com.sun.javaws.HtmlOptions;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.security.SecureRandom;
  10. /**
  11. * @author Zhtao
  12. * @date 2022/2/25 19:56
  13. */
  14. @WebServlet(value = "/rs")
  15. public class RegisterServlet extends HttpServlet {
  16. // 1.获取用户请求发送的数据
  17. @Override
  18. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  19. String username = req.getParameter("username");
  20. String password = req.getParameter("password");
  21. System.out.println("用户提交的数据:" + username + "\t" + password);
  22. }
  23. }

Servlet - 图5

Servlet - 图6

3.解决中文乱码

req.setCharacterEncoding(“UTF-8”);

  1. package com.qf.servlet2;
  2. import com.sun.javaws.HtmlOptions;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.security.SecureRandom;
  10. /**
  11. * @author Zhtao
  12. * @date 2022/2/25 19:56
  13. */
  14. @WebServlet(value = "/rs")
  15. public class RegisterServlet extends HttpServlet {
  16. @Override
  17. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. // 对request请求对象设置统一编码
  19. req.setCharacterEncoding("UTF-8");
  20. // 1.获取用户请求发送的数据
  21. String username = req.getParameter("username");
  22. String password = req.getParameter("password");
  23. System.out.println("用户提交的数据:" + username + "\t" + password);
  24. }
  25. }

3.response响应数据

1.response主要方法

方法名称 作用
setHeader(name,value) 设置响应信息头
setContenType(String) 设置响应文件数据、响应式的编码格式
setCharacterEncoding(Sting) 设置服务端响应内容编码格式
getWriter() 获取字符输出流

例:用户注册后,返回注册成功!

  1. package com.qf.servlet2;
  2. import com.sun.javaws.HtmlOptions;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.security.SecureRandom;
  11. /**
  12. * @author Zhtao
  13. * @date 2022/2/25 19:56
  14. */
  15. @WebServlet(value = "/rs")
  16. public class RegisterServlet extends HttpServlet {
  17. @Override
  18. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  19. // 对request请求对象设置统一编码
  20. req.setCharacterEncoding("UTF-8");
  21. // 1.获取用户请求发送的数据
  22. String username = req.getParameter("username");
  23. String password = req.getParameter("password");
  24. System.out.println("用户提交的数据:" + username + "\t" + password);
  25. // 2.响应数据给客户端
  26. PrintWriter printWriter = resp.getWriter();
  27. printWriter.println("Register Success!");
  28. }
  29. }

Servlet - 图7

2.解决返回中文乱码

方法1(不推荐):

  1. resp.setCharacterEncoding("utf-8"); // 设置服务器端编码格式
  2. resp.setHeader("Content-Type","text/html;charset=utf-8"); // 设置客户端编码方式

方法2:

放到PrintWriter printWriter = resp.getWriter();后面

  1. resp.setContentType("text/html;charset=UTF-8");