前言
(113条消息) Servlet基础详细版--手动实现Servlet(idea)_我爱布朗熊的博客-CSDN博客
接着上面链接继续
目录
三、使用IDEA创建Servlet程序(这种方式更快更好用)
获取请求的方式、做判断、做处理(三步)
在webapp包下创建A.html程序(如果放在WEB-INF下回出现错误)
- <body>
- <form action="http://localhost:8080//web_Tomcat_war_exploded/hello" method="get">
- <input type="submit">
-
- form>
-
- body>
运行项目,然后进入到A.html页面之下
点击提交,控制台会出现下面的现象
第一次点击提交查询时,会运行构造方法,初始方法以及service方法,以后若在点击提交查询按钮时,仅仅运行service方法
- <form action="http://localhost:8080//web_Tomcat_war_exploded/hello" method="post">
- <input type="submit">
-
- form>
其他操作和GET操作一样
我们发现效果也和GET一样
既然GET请求和POST请求都会运行service方法,那我们怎么辨别呢?
- /**
- * service方法专门用来处理请求和响应的
- * @param servletRequest
- * @param servletResponse
- * @throws ServletException
- * @throws IOException
- */
- @Override
- public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
-
- System.out.println("3 service方法");
-
- // 将类型强转换(因为他有getMethod方法)
- HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
-
- // 获取请求的方式
- String method = httpServletRequest.getMethod();
-
- System.out.println(method);
-
- if("GET".equals(method)){
-
- System.out.println("处理GET请求");
-
- }else if("POST".equals(method)){
-
- System.out.println("处理POST请求");
-
- }
-
- }
通过上面getMethod()方法我们便可以区别开GET请求和POST请求,但是上面那个代码后续不好维护,我们可以再修改的不臃肿一点。
将GET处理和POST处理分别写成一个方法。
- /**
- * service方法专门用来处理请求和响应的
- * @param servletRequest
- * @param servletResponse
- * @throws ServletException
- * @throws IOException
- */
- @Override
- public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
- System.out.println("3 service方法");
- // 将类型强转换(因为他有getMethod方法)
- HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
- // 获取请求的方式
- String method = httpServletRequest.getMethod();
- System.out.println(method);
- if("GET".equals(method)){
- doGet();
- }else if("POST".equals(method)){
- doPOST();
- }
-
- }
-
- // 处理GET请求
- public void doGet(){
- System.out.println("处理GET请求");
- }
- // 处理POST请求
- public void doPOST(){
- System.out.println("处理POST请求");
- }
这是从前实现Servlet程序的实现
(113条消息) Servlet基础详细版--手动实现Servlet(idea)_我爱布朗熊的博客-CSDN博客
一般在实际项目开发中,都是使用继承HttpServlet类的方式去实现Servlet程序
1.编写一个类去继承HttpServlet类
2.根据业务需要重写doGet或doPost方法
3.到web.xml中配置Servlet程序的访问地址
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <form action="http://localhost:8080//web_Tomcat_war_exploded/hello2" method="post">
- <input type="submit">
-
- form>
-
- body>
- html>
- //Http这个类中已经将get请求和post请求分发好了
- public class Servlet extends HttpServlet {
-
- /**
- * doGet()在get请求的时候调用
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- System.out.println("hello doGet方法");
- }
-
- /**
- * doPost()在post请求的时候调用
- * @param req
- * @param resp
- * @throws ServletException
- * @throws IOException
- */
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- System.out.println("hello doPost方法");
- }
- }
- <servlet>
- <servlet-name>Servletservlet-name>
- <servlet-class>com.company.web_tomcat.Servletservlet-class>
- servlet>
-
- <servlet-mapping>
- <servlet-name>Servletservlet-name>
- <url-pattern>/hello2url-pattern>
- servlet-mapping>
效果图
这种方式更快,直接帮我们生成
下图中 New->Servlet 直接生成
选用非注解的形式创建
- <servlet>
- <servlet-name>Servlet3servlet-name>
- <servlet-class>com.company.web_tomcat.Servlet3servlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>Servlet3servlet-name>
- <url-pattern>/hello4url-pattern>
- servlet-mapping>
测试
- <body>
- <form action="http://localhost:8080//web_Tomcat_war_exploded/hello4" method="post">
- <input type="submit">
-
- form>
-
- body>
- public class Servlet3 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- System.out.println("hello doGet");
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- System.out.println("hello doPost");
- }
- }