您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“spring mvc怎么實(shí)現(xiàn)頁面訪問”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“spring mvc怎么實(shí)現(xiàn)頁面訪問”吧!
servlet的定義
Servlet is a technology which is used to create a web application. servlet是一項(xiàng)用來創(chuàng)建web application的技術(shù)。
Servlet is an API that provides many interfaces and classes including documentation. servlet是一個(gè)提供很多接口和類api及其相關(guān)文檔。
Servlet is an interface that must be implemented for creating any Servlet.servlet是一個(gè)接口,創(chuàng)建任何servlet都要實(shí)現(xiàn)的接口。
Servlet is a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests. servlet是一個(gè)實(shí)現(xiàn)了服務(wù)器各種能力的類,對(duì)請(qǐng)求做出響應(yīng)。它可以對(duì)任何請(qǐng)求做出響應(yīng)。
Servlet is a web component that is deployed on the server to create a dynamic web page.servlet是一個(gè)web組件,部署到一個(gè)web server上(如tomcat,jetty),用來產(chǎn)生一個(gè)動(dòng)態(tài)web頁面。
servlet的歷史
web Container
web容器也叫servlet容器,負(fù)責(zé)servlet的生命周期,映射url請(qǐng)求到相應(yīng)的servlet。
A web container (also known as a servlet container;[1] and compare "webcontainer"[2]) is the component of a web server that interacts with Java servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access-rights.A web container handles requests to servlets, JavaServer Pages (JSP) files, and other types of files that include server-side code. The Web container creates servlet instances, loads and unloads servlets, creates and manages request and response objects, and performs other servlet-management tasks.A web container implements the web component contract of the Java EE architecture. This architecture specifies a runtime environment for additional web components, including security, concurrency, lifecycle management, transaction, deployment, and other services.
常見的web容器如下:
在web容器中,web應(yīng)用服務(wù)器的結(jié)構(gòu)如下:
1.普通servlet實(shí)現(xiàn)頁面訪問
1.1 實(shí)例1:使用web.xml實(shí)現(xiàn)一個(gè)http服務(wù)
實(shí)現(xiàn)一個(gè)簡單的servlet
package com.howtodoinjava.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyFirstServlet extends HttpServlet { private static final long serialVersionUID = -1915463532411657451L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { // Write some content out.println("<html>"); out.println("<head>"); out.println("<title>MyFirstServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h3>Servlet MyFirstServlet at " + request.getContextPath() + "</h3>"); out.println("</body>"); out.println("</html>"); } finally { out.close(); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Do some other work } @Override public String getServletInfo() { return "MyFirstServlet"; } }
web.xml配置servlet
/MyFirstServlet MyFirstServlet com.howtodoinjava.servlets.MyFirstServlet MyFirstServlet /MyFirstServlet
1.2 編程方式實(shí)現(xiàn)一個(gè)http服務(wù)請(qǐng)求
不需要xml
package com.journaldev.first; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class FirstServlet */ @WebServlet(description = "My First Servlet", urlPatterns = { "/FirstServlet" , "/FirstServlet.do"}, initParams = {@WebInitParam(name="id",value="1"),@WebInitParam(name="name",value="pankaj")}) public class FirstServlet extends HttpServlet { private static final long serialVersionUID = 1L; public static final String HTML_START="<html><body>"; public static final String HTML_END="</body></html>"; /** * @see HttpServlet#HttpServlet() */ public FirstServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); Date date = new Date(); out.println(HTML_START + "<h3>Hi There!</h3><br/><h4>Date="+date +"</h4>"+HTML_END); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
2.spring mvc實(shí)現(xiàn)頁面訪問
2.1 web.xml方式
示例:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Gradle + Spring MVC Hello World + XML</display-name> <description>Spring MVC web application</description> <!-- For web context --> <servlet> <servlet-name>hello-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- For root context --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-core-config.xml</param-value> </context-param> </web-app>
2.2 編碼方式
public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); // Manage the lifecycle of the root application context container.addListener(new ContextLoaderListener(rootContext)); // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(DispatcherConfig.class); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }
內(nèi)部實(shí)現(xiàn)
3.spring boot
繼承了spring mvc的框架,實(shí)現(xiàn)SpringBootServletInitializer
package com.mkyong; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class SpringBootWebApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringBootWebApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } }
然后controller
package com.mkyong; import java.util.Map; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class WelcomeController { // inject via application.properties @Value("${welcome.message:test}") private String message = "Hello World"; @RequestMapping("/") public String welcome(Map<String, Object> model) { model.put("message", this.message); return "welcome"; } }
到此,相信大家對(duì)“spring mvc怎么實(shí)現(xiàn)頁面訪問”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。