javaWeb之Servlet

it2022-05-05  113

Servlet介绍

Servlet是sun 公司提供的一门用于开发动态web资源的技术。用户若想开发一个动态Web资源(即开发一个java程序想向浏览器去输出数据),需要完成2个步骤: 1.编写一个java类,实现servlet接口 2.把开发好的Java类部署到Web服务器中通常我们把实现了servlet接口的java程序称之为Servlet

Servlet运行过程

Servlet实现类

使用Idea开发Servlet

Servlet映射路径问题

1.一般情况下我们都会指定一个URL , 一个URL对应一个请求 2.通配问题 /* :不报错 *.do

转发和重定向的区别 (面试常见题:笔试)

重定向:服务器告诉客户端,你去请求另外一个地址;客户端行为

地址栏会变不能携带参数

转发:服务器自己转发到服务器上的另一个请求; 服务器行为

地址栏不会变可以携带参数 将这个请求,转换到另一个地址;

ServletContext

图示: 案例一: src:

package com.kuang.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 ServletTest01 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我接受到你的请求了"); } } package com.kuang.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; public class SetvletTest02 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /* //告诉浏览器,我们响应的东西使用html解析; resp.setContentType("text/html"); resp.setCharacterEncoding("UTF-8"); //通过响应给客户端返回一个字符串 PrintWriter writer = resp.getWriter(); writer.print("你好!"); writer.print("<h1>Hello,Servlet!</h1>"); */ resp.sendRedirect("index.jsp"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("Post"); } } package com.kuang.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 Failed extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.sendRedirect("404.jsp"); System.out.println("123123123"); } }

web下:404和index.jsp web.xml:

<?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>ServletTest01</servlet-name> <servlet-class>com.kuang.servlet.ServletTest01</servlet-class> </servlet> <servlet> <servlet-name>Test02</servlet-name> <servlet-class>com.kuang.servlet.SetvletTest02</servlet-class> </servlet> <servlet> <servlet-name>404</servlet-name> <servlet-class>com.kuang.servlet.Failed</servlet-class> </servlet> <!-- 关于请求路径问题: localhost:8080/项目名/请求的路径 localhost:8080/servlet01/ --> <servlet-mapping> <servlet-name>ServletTest01</servlet-name> <url-pattern>/test01</url-pattern> </servlet-mapping> <!-- /* /*.do /item/马云 【注意】尽量在请求的URL中不要使用中文; --> <!-- RestFul风格 localhost:8080/项目名/第一级(第一个参数)/第二级(第二个参数)/... localhost:8080/项目名/hello?username=admin&password=123456 localhost:8080/项目名/hello/admin/123456 --> <servlet-mapping> <servlet-name>Test02</servlet-name> <url-pattern>/test02</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>404</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>

案例二: src:

package com.kuang.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; public class ServletDemo01 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //通过servlet对象可以获取一个ServletContext; ServletContext context = this.getServletContext(); String username = "小学生";//可以通过JDBC去读取; //setAttribute :设置一个属性;(属性名,属性值) context.setAttribute("name",username); response.setCharacterEncoding("utf-8"); response.getWriter().print("设置name成功:"+username); } } package com.kuang.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; public class ServletDemo02 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String name = (String) context.getAttribute("name"); resp.setCharacterEncoding("utf-8"); resp.getWriter().print("我们获取到的ServletDemo01信息为:"+name); } }

web.xml:

<?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>Demo01</servlet-name> <servlet-class>com.kuang.servlet.ServletDemo01</servlet-class> </servlet> <servlet> <servlet-name>Demo02</servlet-name> <servlet-class>com.kuang.servlet.ServletDemo02</servlet-class> </servlet> <!--=====================================================--> <servlet-mapping> <servlet-name>Demo01</servlet-name> <url-pattern>/q1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Demo02</servlet-name> <url-pattern>/q2</url-pattern> </servlet-mapping> </web-app>

案例三:通过servletcontext读取网站的配置信息 新建一个properties文件:

driver=com.mysql.jdbc.Driver username=root password=123456 url=jdbc:mysql://localhost:3306/smbms

编写servlet类:

package com.kuang.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; //读取properties配置文件 public class ServletDemo03 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //读取配置文件 //1.获得配置文件的路径 String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/resources/database.properties"); System.out.println("取得的路径为:"+realPath); Properties properties = new Properties(); FileInputStream is = new FileInputStream(realPath); properties.load(is);//把文件流加载到配置文件的对象中; String driver = properties.getProperty("driver"); String username = properties.getProperty("username"); String password = properties.getProperty("password"); String url = properties.getProperty("url"); //响应到网页 resp.getWriter().println(driver); resp.getWriter().println(username); resp.getWriter().println(password); resp.getWriter().println(url); //======================================= System.out.println(driver); System.out.println(username); System.out.println(password); System.out.println(url); } }

配置web.xml文件:

<servlet> <servlet-name>Demo03</servlet-name> <servlet-class>com.kuang.servlet.ServletDemo03</servlet-class> </servlet> <servlet-mapping> <servlet-name>Demo03</servlet-name> <url-pattern>/q3</url-pattern> </servlet-mapping>

访问查看:

localhost:8080/demo02/q3

乱码问题解决方案:

所有能设置编码的地方都统一设置 : 数据库,数据库字段,Servlet程序,请求,响应 , 前端,浏览器。

最新回复(0)