标签:第一个 get text classpath frame ack pack meta html 4.01
在Spring第一个Demo程序的基础上
第一步:添加web.xml文件, web.xml文件放在/WEB-INF下,内容如下
<?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_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 封装了一个监听器,帮助加载spring的配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
该xml在tomcat运行时,会被加载,并且会把applicationContext.xml进行加载
第二步:添加StudentServlet类
package com.fd.spring.servlet; import java.io.IOException; import javax.servlet.ServletConfig; 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 org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; @WebServlet("/student") public class StudentServlet extends HttpServlet{ @Override public void init(ServletConfig config) throws ServletException { super.init(config); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("service..."); req.setAttribute("list", "123"); req.getRequestDispatcher("index.jsp").forward(req, resp); } }
这时运行run on server的话,在页面上访问 http://localhost:8080/SpringDemo/student,在console上可以看到如下信息
第三步:继续添加index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>this is spring on Tomcat</h1> </body> </html>
再次运行出现如下页面
标签:第一个 get text classpath frame ack pack meta html 4.01
原文地址:https://www.cnblogs.com/spark-quant/p/9688642.html