接着昨天创建的maven webapp项目继续玩,先体验体验。
- package com.wyh.servlet;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.io.PrintWriter;
-
- /**
- * @author WuYongheng
- * @date 2022/8/26
- * @description
- */
- public class HelloServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //响应的类型:html
- resp.setContentType("text/html");
- resp.setCharacterEncoding("UTF-8");
- //获取响应的输出流
- PrintWriter out = resp.getWriter();
- out.println("");
- out.println("");
- out.println("
Hello Servlet! "); - out.println("");
- out.println("");
- out.println("
Hello Servlet!
"); - out.println("
你好 Servlet!
"); - out.println("");
- out.println("");
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
- }
- }
1、这里HttpServlet刚开始会标红,提示导包,alt+enter选择add搜索看看本地库里有没有servlet包?
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>4.0.1version>
- <scope>providedscope>
- dependency>
2、 为了显示出中文,需要在doGet请求中配置编码:UTF-8,因为在ISO 8859-1编码中,中文是乱码。
resp.setCharacterEncoding("UTF-8");
servlet是sun公司开发动态web的一门技术
Sun在这些API中提供一个接口叫做:Servlet,如果你想开发一个Servlet程序,只需要完成两个小步骤:
把实现了Servlet接口的Java程序叫做:Servlet
Servlet接口Sun公司有两个默认的实现类:HttpServlet GenericServlet
创建一普通的Maven项目,删除src目录,这样就可以基于这个普通的Maven建Model,这个空工程就是Maven的主工程,在主工程中导入依赖,之后在新建的Model中不用重复导入。
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>4.0.1version>
- <scope>providedscope>
- dependency>
- package com.wyh.servlet;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.io.PrintWriter;
-
- /**
- * @author WuYongheng
- * @date 2022/8/26
- * @description
- */
- public class HelloServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- PrintWriter writer = resp.getWriter(); // 响应流
-
- writer.println("Hello Servlet!");
- }
-
- @Override
- protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req, resp);
- }
- }
-
- <servlet>
- <servlet-name>helloservlet-name>
- <servlet-class>com.wyh.servlet.HelloServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>helloservlet-name>
- <url-pattern>/wyhurl-pattern>
- servlet-mapping>
我们写的是Java程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要在web服务器中注册我们写的Servlet,还需要给他一个浏览器能够访问的路径。
注意:配置项目发布的路径
观察url
父项目中的jar包,子项目可以直接使用,和Java中的继承差不多。创建父子工程,简化操作了
1、一个Servlet可以指定一个路径
2、一个Servlet可以指定多个路径
3、一个Servlet可以指定通用路径,这样会干掉index.jsp,不建议使用
- <servlet-mapping>
- <servlet-name>helloservlet-name>
- <url-pattern>/*url-pattern>
- servlet-mapping>
4、一个Servlet可以自定义后缀实现请求映射,注意,*前面不能剧啊项目的映射路径
- <servlet-mapping>
- <servlet-name>helloservlet-name>
- <url-pattern>*.wyhurl-pattern>
- servlet-mapping>
5、优先级问题
web容器在启动的时候,它会为每一个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用。
- package com.wyh.servlet;
-
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- /**
- * @author WuYongheng
- * @date 2022/8/29
- * @description
- */
- public class HelloServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- ServletContext context = this.getServletContext();
- String username = "巫永恒";
- // 将数据保存在了Servlet中,名字为:username。值 username
- context.setAttribute("username", username);
- }
-
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req, resp);
- }
- }
- package com.wyh.servlet;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- /**
- * @author WuYongheng
- * @date 2022/8/29
- * @description
- */
- public class GetContext extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- ServletContext context=this.getServletContext();
- String username= (String) context.getAttribute("username");
-
- resp.setContentType("text/html");
- resp.setCharacterEncoding("utf-8");
- resp.getWriter().print("名字"+username);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req, resp);
- }
- }
- <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"
- metadata-complete="true">
-
- <servlet>
- <servlet-name>helloservlet-name>
- <servlet-class>com.wyh.servlet.HelloServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>helloservlet-name>
- <url-pattern>/wyhurl-pattern>
- servlet-mapping>
-
- <servlet>
- <servlet-name>getcservlet-name>
- <servlet-class>com.wyh.servlet.GetContextservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>getcservlet-name>
- <url-pattern>/getcurl-pattern>
- servlet-mapping>
-
- web-app>
-
直接访问/getc,拿不到名字。
先访问/wyh,将数据放到ServletContext里面,再访问/getc拿到数据