编写一个Servlet程序
编写一个普通类
实现Servlet接口,(继承接口的实现类HttpServlet)
在web.xml文件注册
在浏览器上写请求,请求注册的name就会跳转到响应的类(浏览器所显示的网页其实就是服务器上的一个文件夹里的文件)
package com.kuang; 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; //继承接口的实现类HttpServlet public class HttpScerelet extends HttpServlet { @Override //重写方法 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter(); //响应流 writer.println("hello word"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
编写了类之后需要编写Servlet映射(我们编写的java程序需要通过浏览器访问,浏览器需要链接web服务器,所以我们需要在web服务中注册我们写的Servlet 还需要给他一个浏览器能够访问的路径)
<!--注册servlet--> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.kuang.HttpScerelet</servlet-class> </servlet> <!--servlet请求路径--> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
配置Tomcat :配置项目发布的路径
运行并进入网页之后 /打上web.xml里配置的那个hello,他就去找文件里映射的hello并最终找到HttpServlet类里输出的 hello word
Servlet原理
浏览器–>web容器–>(请求(头,体Requset),响应(头,体Response))–使用Servlet的Service方法–>Servlet—>我们自己编写的实现类 再原路返回
Mapping
一个Servlet可以指定一个,多个指定,通用,映射路径
指定一些前缀
<!--servlet请求路径 多个也可以--> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet-mapping> //多个也可以 <servlet-name>hello</servlet-name> <url-pattern>/hello01</url-pattern> </servlet-mapping> <servlet-mapping> //乱写的也可以访问 <servlet-name>hello</servlet-name> <url-pattern>/hello/*</url-pattern> </servlet-mapping> <servlet-mapping> //默认的 *前面不能加映射路径 <servlet-name>hello</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <servlet-mapping> //可以自定义后缀实现请求映射 <servlet-name>hello</servlet-name> <url-pattern>*.qinjiang</url-pattern> </servlet-mapping>
//
优先级问题
制定了固有的映射路径优先级最高,找不到就会走默认的路径(处理错误的请求)
<!--404--> // <servlet> <servlet-name>error</servlet-name> <servlet-class>com.kuang.ErrorServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>error</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> ---------------------------------------------------------------- package com.kuang; //映射的类 import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class ErrorServlet extends HttpScerelet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); PrintWriter writer = resp.getWriter(); writer.print("<h1>肆零肆<h1>"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
ServletContext
web容器刚启动的时候为每个web程序都创建一个对应ServletContext对象,他代表当前的web应用
共享数据(我在这个Servlet中保存的数据,可以在另一个servlet中找到)
public class HelloServlet extends HttpServlet { //先创建一个放置数据的类 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext() ; String username = "秦将"; //数据 context.setAttribute("username",username);//将一个数据保存在了ServletContext中,名字为username 值为username System.out.println("Hello"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } public class GetServlet extends HelloServlet{ //还需要一个读取他的类 @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); } } <servlet> //配置他们的XML文件 <servlet-name>hello</servlet-name> <servlet-class>com.kuang.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>hello01</servlet-name> <servlet-class>com.kuang.servlet.GetServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello01</servlet-name> <url-pattern>/gecc</url-pattern> </servlet-mapping>
最后测试访问的结果
ServletContext应用
public class ServletContextdemo extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String url = context.getInitParameter("url"); resp.getWriter().print(url); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } web.xml <!--配置一些web用用的初始化参数--> 获取初始化参数 <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306 </param-value> </context-param> 注册web <servlet> <servlet-name>gp</servlet-name> <servlet-class>com.kuang.servlet.ServletContextdemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>gp</servlet-name> <url-pattern>/gp</url-pattern> </servlet-mapping>
请求转发context.getRequestDispatcher(“/gp”).forward(req,resp); //上述的东西和成
读取资源文件
public class Servletdemo05 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //把文件变成一个流 InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/ac.properties");//把文件变成一个流 Properties prop = new Properties(); prop.load(is); //获取这个文件的流 String us= prop.getProperty("username"); String pa=prop.getProperty("password"); resp.getWriter().print(us); //输出 } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } web.xml文件 <servlet> <servlet-name>sd5</servlet-name> <servlet-class>com.kuang.servlet.Servletdemo05</servlet-class> </servlet> <servlet-mapping> <servlet-name>sd5</servlet-name> <url-pattern>/sd5</url-pattern> </servlet-mapping>