Servlet03¶
11.练习¶
快捷键-可以快速地在访问的文件件切换
ctrl+alt+向左箭头:回到上次访问的位置
ctrl+alt+向右箭头:回到下一步访问的位置
11.1CatServlet¶
首先创建项目servlet,配置好Tomcat,添加web应用支持。在web目录下面的WEB-INF目录下创建lib目录,在Tomcat安装包的lib目录中找到servlet-api,并添加到idea的web目录下面的WEB-INF/lib目录,选择引用为库文件
在src目录下创建目录,编写CatServlet类:
package servlet;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class CatServlet implements Servlet {
public int count = 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 {
count++;//每次访问service方法,count累加
System.out.println("该servlet访问次数=" + count);
//获取请求的方式
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String method = httpServletRequest.getMethod();
if (method.equals("GET")) {
doGet();
} else if (method.equals("POST")) {
doPost();
}
}
public void doGet() {
System.out.println("CatServlet doGet()...");
}
public void doPost() {
System.out.println("CatServlet doPost()...");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
在web.xml文件中进行Servlet配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>CatServlet</servlet-name>
<servlet-class>servlet.CatServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CatServlet</servlet-name>
<url-pattern>/cat</url-pattern>
</servlet-mapping>
</web-app>
在WEB-INF目录下编写一个html,用于测试
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="http://localhost:8080/servlet/cat" method="get">
name: <input type="text"/><br/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
点击启动Tomcat,在浏览器中输入地址:http://localhost:8080/servlet/login.html
点击login按钮,成功跳转到CatServlet中:
后台输出:(如果表单方式改变了,仍然调用的是旧的访问方法,点击保存html,并刷新浏览器页面即可解决)
11.2DogServlet¶
在11.1的项目框架上进行编程
首先在src目录下创建DogServlet类
package 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 DogServlet extends HttpServlet {
private int getCount = 0;
private int postCount = 0;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
getCount++;
System.out.println("getCount=" + getCount);
System.out.println("DogServlet-访问doGet()");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
postCount++;
System.out.println("postCount=" + postCount);
System.out.println("DogServlet-访问doPost()");
}
}
注意:这里没有重写service方法,因此当浏览器请求DogServlet时,会去父类HttpServlet类中去找调用的service方法,在HttpServlet类中的service方法会根据请求类型如get、post去调用当前运行类型(即DogServlet)的doPost、doGet方法,因此在自己创建的Servlet中不用判断请求方式
在web.xml文件中配置该Servlet:
<servlet>
<servlet-name>DogServlet</servlet-name>
<servlet-class>servlet.DogServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DogServlet</servlet-name>
<url-pattern>/dog</url-pattern>
</servlet-mapping>
修改之前的login.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="http://localhost:8080/servlet/dog" method="post">
name: <input type="text"/><br/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
点击redeploy Tomcat,在浏览器输入地址http://localhost:8080/servlet/login.html
,点击login按钮,成功跳转到DogServlet
将login.html文件中表单提交方式反复该为post或者get,多次访问,后台输出如下:
(如果表单方式改变了,仍然调用的是旧的访问方法,点击保存html,并刷新浏览器页面即可解决)
11.3PigServlet¶
在11.1的项目框架上进行编程
在src目录下编写PigServlet
package servlet;
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;
@WebServlet(urlPatterns = {"/pig1", "/pig2"},loadOnStartup = 1)
public class PigServlet extends HttpServlet {
private int getCount = 0;
private int postCount = 0;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
getCount++;
System.out.println("PigServlet-访问doGet()-get访问次数=" + getCount);
System.out.println("客户端地址="+req.getRemoteAddr());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
postCount++;
System.out.println("PigServlet-访问doPost()-post访问次数=" + postCount);
System.out.println("客户端地址="+req.getRemoteAddr());
}
}
修改login.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form action="http://localhost:8080/servlet/pig1" method="get">
name: <input type="text"/><br/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
点击redeploy Tomcat,在浏览器输入地址http://localhost:8080/servlet/login.html
,点击login按钮,成功跳转到PigServlet
将login.html文件中表单提交方式反复改为post或者get,多次访问,后台输出如下:
(如果表单方式改变了,仍然调用的是旧的访问方法,点击保存html,并刷新浏览器页面即可解决)