In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "what is the Java Servlet response httpServletResponse process". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what the Java Servlet response httpServletResponse process is.
1. The core method 1.setStatus
Set response status code if this method is not called, 200 status code is returned by default (premise: normal execution, no exception). If an exception occurs, 500 is returned.
Front-end code:
Set response header
Jump to function toWelcome () {let username= document.querySelector ("# username"); _ window.location.href = "html?type=2&username=" + username.value;}
Backend code:
@ WebServlet ("/ html") public class HTMLTypeServlet extends HttpServlet {/ / html?type=... @ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/ / response html: set the Content-Type resp.setContentType of the response ("text/html; charset=utf-8"); PrintWriter pw = resp.getWriter (); / / get the value String type= req.getParameter ("type") of type in queryString If ("1" .equals (type)) {/ / returns a simple html pw.println ("get web page successfully");} else if ("2" .equals (type)) {/ / returns complex dynamic html / / html?type=2&username=xxx String username= req.getParameter ("username"); pw.println ("
Pw.println ("Welcome," + username); pw.println ("
");}}
Simple:
The front end shows:
Click "View":
Dynamic:
The front end explicitly:
Click "Jump":
About dynamic web pages: in Java code, write a lot of html code
Too coupled (two completely different programming languages, developed together), poor maintainability and expansibility
Solution:
Template technology
There are still some problems in this way, and the further development will lead to the emergence of ajax technology.
Second, respond to a web page
Return to an existing web page
(1) redirect:
Features: the url address bar will change and initiate two requests
Principle:
For the first time, the response status code and response header Location: the address of the web page are returned.
The second time: the browser automatically jumps to the address set by Location
It is still quite common: for example, after a successful login (you can also jump in the js code), jump to a home page
(2) forward:
Features: the url address bar remains unchanged, with only one request
Principle: when requesting Servlet, Servlet gets the html of the forwarding path and sets the content of this path to the response body
Front-end code:
Redirect to hello.html redirect forward to hello.html jump
Backend code:
@ WebServlet ("/ goto") public class GoToServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/ / goto?type=xxx String type= req.getParameter ("type"); if ("1" .equals (type)) {/ / redirect / / resp.setStatus (301); / / resp.setHeader ("Location", "hello.html") / / the above code can be simplified to sendRedirect resp.sendRedirect ("hello.html");} else if ("2" .equals (type)) {/ / forward req.getRequestDispatcher ("hello.html") .forward (req,resp);} III. Return a file
Set up Content-Type, and then put the binary data of the file in the response body
Front-end code:
Get a picture (render display)
Get a music (render display) get a picture (download) download get a music (download)
Backend code:
WebServlet ("/ file") public class FileServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/ /
/ / get the byte output stream of the response object OutputStream os = resp.getOutputStream (); / / the returned file type: 1. Picture 2. Music String type = req.getParameter ("type"); / / Operation on return: 1. Render 2. Download String show = req.getParameter ("show"); File file = null; byte [] data = null; / /
If ("photo" .equals (type)) {/ / returns the image if ("1" .equals (show)) {resp.setContentType ("image/jpeg"); / / jpg format} else {/ / this just does not set the download file name, if you are interested, you can expand to complete the resp.setContentType ("application/octet-stream"). } file = new File ("D:\\ java\\ servlet-study\\ src\\ main\\ resources\\ cui.jpg"); / /} else if ("music" .equals (type)) {/ / returns music if ("1" .equals (show)) {resp.setContentType ("audio/mp3") / / mp3 format} else {resp.setContentType ("application/octet-stream");} file = new File ("D:\\ java\\ servlet-study\\ src\\ main\\ resources\\ there are so many people in the world .mp3") } / / other formats can be expanded to complete / / return a file type: Content-Length,body data = Files.readAllBytes (file.toPath ()); resp.setContentLength (data.length); / / setHeader ("Content-Length", xxx) os.write (data);}}
Question: pictures, music and videos are static files that can be accessed directly under the web app webapp. Do you still need Servlet to return them? Is it superfluous?
If the total size of the file is very large, it is not appropriate to put it under the webapp of the web application: it is more difficult to pack, and it is more appropriate to use Servlet to read files from other parts of the local and return them.
4. Return json data
Often used in ajax requests to return some data for dynamically populating web pages
Front-end code:
Get ajax response data and generate web page content dynamically
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.