Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to access the page with spring mvc

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to access the page with spring mvc". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to access the page with spring mvc".

Definition of servlet

Servlet is a technology which is used to create a web application. Servlet is a technology used to create web application.

Servlet is an API that provides many interfaces and classes including documentation. Servlet is an api that provides many interfaces and classes and related documentation.

Servlet is an interface that must be implemented for creating any Servlet.servlet is an interface that creates an interface that any servlet needs to implement.

Servlet is a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests. Servlet is a class that implements various capabilities of the server and responds to requests. It can respond to any request.

Servlet is a web component that is deployed on the server to create a dynamic web page.servlet is a web component that is deployed on a web server (such as tomcat,jetty) to generate a dynamic web page.

History of servlet

Web Container

The web container, also known as the servlet container, is responsible for the life cycle of the servlet, mapping url requests to the corresponding servlet.

A webcontainer (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.

Common web containers are as follows:

In the web container, the structure of the web application server is as follows:

1. Ordinary servlet implements page access

1.1 example 1: implementing a http service using web.xml

Implement a simple 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 ("); out.println ("); out.println ("MyFirstServlet"); out.println (""); out.println (""); out.println ("Servlet MyFirstServlet at" + request.getContextPath () + "); out.println ("); out.println (");} 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 configuration servlet

/ MyFirstServlet MyFirstServlet com.howtodoinjava.servlets.MyFirstServlet MyFirstServlet / MyFirstServlet

1.2 programmatic implementation of a http service request

No need for 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 FirstServlet", 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= "; public static final String HTML_END=" / * * @ 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 + "Hi hereafter date =" + date + "" + 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 implements page access

2.1 web.xml mode

Example:

Gradle + Spring MVC Hello World + XML Spring MVC web application hello-dispatcher org.springframework.web.servlet.DispatcherServlet contextConfigLocation / WEB-INF/spring-mvc-config.xml 1 hello-dispatcher / org.springframework.web.context.ContextLoaderListener contextConfigLocation / WEB-INF/spring-core-config.xml

2.2 Encoding mode

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 ("/");}}

Internal implementation

3.spring boot

Inherit the framework of spring mvc and implement 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);}}

And then 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 model) {model.put ("message", this.message); return "welcome" }} at this point, I believe you have a deeper understanding of "how to access the page with spring mvc". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report