创建servlet项目
- 新建servlet项目
https://blog.csdn.net/fupinghaohao/article/details/137351065
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
- 运行tomcat
Servlet1.java
package com.example.zlp;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* 实现Servlet规范,继承HttpServlet类
* 处理http请求,重写service方法
* 设置注解,指定访问的路径
*/
//访问路径配置的多种方式,注意路径中的“/”不能忘记
//@WebServlet("/servlet1")
//@WebServlet(name = "servlet01",value = "/servlet1")//http://localhost:8080/SpringServlet2024_1_war_exploded/servlet1
//@WebServlet(name = "servlet01",value = {"/servlet1","/servlet01"})
//@WebServlet(name = "servlet01",urlPatterns = "/servlet1")
@WebServlet(name = "servlet01",urlPatterns = {"/servlet1","/servlet01"})
public class Servlet1 extends HelloServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//打印在控制台
System.out.println("hello service servlet");
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
System.out.println(Charset.defaultCharset());
//常用方法
//获取请求时的完整路径(从http开始到?前结束)
String url=req.getRequestURL()+"";
System.out.println(new String("获取请求时的完整路径".getBytes("UTF-8"), "UTF-8")+url);
//获取请求时的部分路径(从项目的站点名开始到?前结束)
String uri=req.getRequestURI()+"";
System.out.println("获取请求时的部分路径"+uri);
//获取请求时的参数字符串
String query=req.getQueryString();
System.out.println("获取请求时的参数字符串"+query);
//获取请求方式
String method=req.getMethod();
System.out.println("获取请求方式"+method);
//获取当前协议版本
String protocol = req.getProtocol();
System.out.println("获取当前协议版本"+protocol);
//获取项目的站点路径
String webapp=req.getContextPath();
System.out.println("获取项目的站点路径"+webapp);
/*获取请求的参数
* http://localhost:8080/S1/servlet1?uname=wo&upwd=123&hobby=happ&hobby=say
* */
//获取指定名称的参数值
String uname=req.getParameter("uname");
String upwd=req.getParameter("upwd");
System.out.println("uname:"+uname+" upwd:"+upwd );
//获取指定名称的所有参数值
String[] hobbys=req.getParameterValues("hobby");
if(hobbys!=null){
for(String hobby:hobbys){
System.out.println(hobby);
}
}
//通过流输出到浏览器
resp.getWriter().write("hello service servlet 我是servlet");
}
}
添加访问路径:http://localhost:8080/SpringServlet2024_1_war_exploded/servlet1
-
servlet的两种方式
index.isp
<%--注解方式--%>
<a href="hello-servlet">Hello Servlet</a>
<%--web.xml映射方式--%>
<a href="myServlet">Hello MyServlet</a>
HelloServlet.java
package com.example.zlp;
import java.io.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}
web.xml
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--我要从jsp跳到servlet-->
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.example.zlp.Myservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<!--我在jsp中要跳的地方-->
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
Myservlet.java
package com.example.zlp;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class Myservlet extends HttpServlet {
private String message;
public void init() {
message = "Hello Myservlet!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
@Override
public void destroy() {
super.destroy();
}
}