网上写servlet的大神不少,记录一下遇到的问题。
配置是ecplise+tomcat9.0
关于servlet的创建方法:项目右键->new->servlet,然后package和class的名字随便命名,之后全部默认或者点finish就好。
- servlet配置现在有两种,网上比较多的是用xml来的,但是新版的可以通过 @配置注解,新生成的servlet是自带的,不需要任何配置
- 一开始找到了一个直接用service的,但似乎它直接扩展了底层的内容,可以用doget和dopost来解决
- 中文字符乱码问题,需要设置response.setCharacterEncoding(“UTF-8”)和request.setCharacterEncoding(“UTF-8”)。需要放在最前面,否则可能在传入值了以后就没用了。
这里写的是一个实现登录功能的表单提交的servlet,项目名字叫做Servlet_try4,表单一共就user和password两个内容,最后跳转到hellp.jsp显示登录状态
关于表单部分,在form里加上这个就好
1
| <form action="servlet" method="post">
|
至于表单内部的内容,记得写上name,servlet是根据name来获取的。
原来代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package servlet.study;
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet("/Servlet_try4") public class Servlet_try4 extends HttpServlet { private static final long serialVersionUID = 1L;
public Servlet_try4() { super(); }
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf8"); String username = request.getParameter("user"); String password = request.getParameter("password");
request.setAttribute("user", username); request.setAttribute("password", password);
RequestDispatcher dispatcher = request.getRequestDispatcher("/hello.jsp"); dispatcher.forward(request, response); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
}
|
改进代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package servlet.study;
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet("/Servlet_try4") public class Servlet_try4 extends HttpServlet { private static final long serialVersionUID = 1L;
public Servlet_try4() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("user"); String password = request.getParameter("password");
request.setAttribute("user", username); request.setAttribute("password", password); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
doGet(request, response);
RequestDispatcher dispatcher = request.getRequestDispatcher("/hello.jsp"); dispatcher.forward(request, response); }
}
|
唔。。虽然这个可以成功,但是在后续学习上发现对于表单提交,最好把doGet的内容全部写到doPost里,这里就不更新了,但是希望可以注意一下~
附:doGet()和doPost()的区别
ps.这里只能实现通过submit做一个表单提交,但是对于很多网页来说需要或许数据库也要调用servlet,又或者想要在提交前做一些什么判断,这样子的话可以尝试一下ajax来跳转到servlet。
搬运自CSDN:https://blog.csdn.net/yueyue200830/article/details/84984287