#SpringMVC:使用原生的Servlet API #HttpServletRequest、HttpServletResponse @FDDLC
專案結構:
pom.xml的內容:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>P049_ServletAPI</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>P049_ServletAPI Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.8.RELEASE</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>servlet-api</artifactId> <version>6.0.36</version> </dependency> </dependencies> <build> <finalName>P049_ServletAPI</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
web.xml的內容:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--關聯springmvc.xml,讓其中的配置生效!--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
springmvc.xml的內容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--開啟對Spring MVC註解的支援--> <mvc:component-scan base-package="cn.liuxingchang.controller"></mvc:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
index.jsp的內容:
<html> <body> <a href="test?name=Tom&age=20">test</a> </body> </html>
success.jsp的內容:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>Success!</h3> </body> </html>
HelloController.java的內容:
package cn.liuxingchang.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Enumeration; @Controller public class HelloController { @RequestMapping("/test") public String test(HttpServletRequest request, HttpServletResponse response) { Enumeration parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String parameterName = (String) parameterNames.nextElement(); System.out.println(request.getParameter(parameterName)); } return "success"; } }
相關文章
- http協議、HttpServletRequest、HttpServletResponseHTTP協議Servlet
- JavaWeb Servlet Http協議 HttpServletRequestJavaWebServletHTTP協議
- 如何優雅地讀寫HttpServletRequest和HttpServletResponse的請求體HTTPServlet
- Servlet技術12_HttpServletRequest類ServletHTTP
- SpringMVC:@ResponseBody註解與HttpServletResponse物件SpringMVCHTTPServlet物件
- 請問怎麼透過HttpServletRequest和HttpServletResponse 物件取得ServletContext物件HTTPServlet物件Context
- 請問怎麼透過HttpServletRequest或HttpServletResponse物件取得ServletContext物件HTTPServlet物件Context
- rocket 原生 API 使用API
- SpringMVC 回顧servletSpringMVCServlet
- react中使用高德地圖的原生APIReact地圖API
- SpringMVC入門案例 & 常用API使用演示SpringMVCAPI
- springMvc原始碼學習之:利用springMVC隨時隨地獲取HttpServletRequest等物件SpringMVC原始碼HTTPServlet物件
- SpringMVC框架和Servlet開發有啥子區別(SpringMVC優勢)SpringMVC框架Servlet
- 基於servlet+原生dbc的登入案例(含servlet知識點梳理)Servlet
- SpringMVC是如何逐步簡化Servlet的程式設計的SpringMVCServlet程式設計
- 使用JavaScript呼叫手機平臺上的原生APIJavaScriptAPI
- servlet寫服務端APIServlet服務端API
- JavaWeb開發基礎Servlet APIJavaWebServletAPI
- 精讀《結合 React 使用原生 Drag Drop API》ReactAPI
- 如何忘卻jQuery,開始使用JavaScript原生APIjQueryJavaScriptAPI
- 使用RequestParam或HttpServletRequest獲取上傳的檔案HTTPServlet
- HttpServletRequest的常見方法HTTPServlet
- springmvc 獲取當前請求的 原生request/responseSpringMVC
- #Spring JdbcTemplate入門@FDDLCSpringJDBC
- springmvc學習指南 之---第27篇 spring如何實現servlet3.0無web.xml 配置servlet物件的SpringMVCServletWebXML物件
- js原生api之String的slice方法JSAPI
- JavaScript Sanitizer API:原生WEB安全API出現啦JavaScriptAPIWeb
- 淺嘗Spring註解開發_Servlet3.0與SpringMVCServletSpringMVC
- HttpServletRequest使用者請求與cookie詳解HTTPServletCookie
- Java Web之Struts2訪問Servlet APIJavaWebServletAPI
- Eureka詳解系列(二)--如何使用Eureka(原生API,無Spring)APISpring
- 使用Node.js原生API寫一個web伺服器Node.jsAPIWeb伺服器
- 【Myself-Security】SpringMVC 開發 RESTFul APISpringMVCRESTAPI
- #SpringBoot入門程式 @FDDLCSpring Boot
- HttpServletRequest 獲取 CookieHTTPServletCookie
- HttpServletRequest常用獲取URL的方法HTTPServlet
- springMVC基本使用SpringMVC
- Struts2 直接訪問Servlet API(二十五)ServletAPI