赞
踩
上个文件有详细介绍
package HelloServlet; import javax.servlet.*; import java.io.IOException; /** * @author 孙显圣 * @version 1.0 */ public class HelloServlet implements Servlet { /** * 当创建HelloServlet会调用,且只会调用一次 * @param servletConfig * @throws ServletException */ @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("init()"); } /** * 返回ServletConfig,也就是servlet的配置 * @return */ @Override public ServletConfig getServletConfig() { return null; } /** * 1.该方法处理浏览器的请求 * 2.当浏览器每次请求一次servlet时,就会调用一次 * 3.当tomcat调用该方法时,会把http请求的数据封装成实现ServletRequest接口的request对象 * 4.通过servletRequest对象,可以得到用户提交的数据 * 5.ServletResponsed对象可以用于返回数据给tomcat服务器 * @param servletRequest * @param servletResponse * @throws ServletException * @throws IOException */ @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("hi, helloServlet"); } /** * 返回servlet的信息,使用较少 * @return */ @Override public String getServletInfo() { return null; } /** * 1.该方法是servlet销毁时调用的 * 2.只会调用一次 */ @Override public void destroy() { } }
@Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { //GET和POST的分发处理 //1.首先需要知道,ServletRequest的运行类型其实是它的子类HttpServletRequest的实现类,而getMethod方法是这个子接口的,所以需要将这个父接口转换为子接口的类型 HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; //现在就是他的子接口,指向它的实现类,封装了请求的方法 String method = httpServletRequest.getMethod(); if (method.equals("GET")) { doGet(); } else if (method.equals("POST")) { doPost(); } } //编写doGet和doPost方法,用于分发处理 public void doGet() { System.out.println("doGet被调用"); } public void doPost() { System.out.println("doPost被调用"); }
<%-- Created by IntelliJ IDEA. User: 8615941515990 Date: 2024/1/24 Time: 16:38 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="http://localhost:8080/servlet/helloServlet" method="post"> username: <input type="text" name="username"> </form> </body> </html>
package HelloServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author 孙显圣 * @version 1.0 */ public class HiServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("hi HiServlet"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
package annotation; 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; /** * @author 孙显圣 * @version 1.0 */ //使用注解配置路由,一个servlet可以有多个路由,访问网址为http://localhost:8080/servlet/ok1 或者http://localhost:8080/servlet/ok2 @WebServlet(urlPatterns = {"/ok1", "/ok2"}) public class OkServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("注解的doGet"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("注解的doPost"); } }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/servlet/cat" method="post">
username:<input name="username" type="text">
</form>
</body>
</html>
package HomeWork; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; /** * @author 孙显圣 * @version 1.0 */ public class CatServlet implements Servlet { private int num = 0; //统计次数 @Override public void init(ServletConfig servletConfig) throws ServletException { } @Override public ServletConfig getServletConfig() { return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("访问次数:" + (++num)); //向下转型获取请求方式 HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; String method = httpServletRequest.getMethod(); if (method.equals("GET")) { System.out.println("GET"); } else if (method.equals("POST")) { System.out.println("POST"); } } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/servlet/dog" method="post">
username:<input name="username" type="text">
</form>
</body>
</html>
package HomeWork; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author 孙显圣 * @version 1.0 */ public class DogServlet extends HttpServlet { //统计次数 private int get = 0; private int post = 0; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("GET:" + (++get)); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("POST:" + (++post)); } }
package HomeWork; 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.net.InetAddress; /** * @author 孙显圣 * @version 1.0 */ @WebServlet(urlPatterns = {"/pig1", "/pig2"}) public class PigServlet extends HttpServlet { //统计次数 private int get = 0; private int post = 0; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("发出请求的浏览器的ip:" + req.getRemoteAddr()); System.out.println("GET:" + (++get)); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("发出请求的浏览器的ip:" + req.getRemoteAddr()); System.out.println("POST:" + (++post)); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。