Java learning notes-HttpServletResponse (23), java learning notes

Source: Internet
Author: User

Java learning notes-HttpServletResponse (23), java learning notes
Obtain client information

1. Write ClientServlet. java

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setCharacterEncoding ("UTF-8"); request. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html; charset = UTF-8"); PrintWriter out = response. getWriter (); // get the client information out. println ("getMethod ():" + request. getMethod () + "<br/>"); out. println ("getRequestURL ():" + request. getRequestURL () + "<br/>"); out. println ("getRequestURI ():" + request. getRequestURI () + "<br/>"); out. println ("getProtocol ():" + request. getProtocol () + "<br/>"); out. println ("getQueryString ():" + request. getQueryString () + "<br/>"); out. println ("getRemoteAddr ():" + request. getRemoteAddr () + "<br/>"); out. println ("getRemoteHost ():" + request. getRemoteHost () + "<br/>"); out. println ("getRemotePort ():" + request. getRemotePort () + "<br/>"); out. println ("getServletPath ():" + request. getServletPath () + "<br/>"); out. println ("getContextPath ():" + request. getContextPath () + "<br/> ");}

The above method can be used to obtain the servlet and servletContext paths through request. In actual development, the names of our websites usually need to be changed. Therefore, if you use the website name in a web project in the future, you must use request. getContextPath () to obtain it, instead of writing it to death. This improves the maintainability of the later stage.

2. Obtain Server Information

Compile a GetServerInfo. java

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setCharacterEncoding ("UTF-8"); request. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html; charset = UTF-8"); PrintWriter out = response. getWriter (); // get the client information out. println ("getLocalAddr ():" + request. getLocalAddr () + "<br/>"); out. println ("getLocalName ():" + request. getLocalName () + "<br/>"); out. println ("getLocalPort ():" + request. getLocalPort () + "<br/> ");}

3. Request forwarding

Http: /localhost: 8080/demo1? Name = jack & password = root

Demo1Servlet for processing user requests

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// get the request parameter String name = request. getParameter ("name"); String psw = request. getParameter ("password"); // determines if (name! = Null & psw! = Null & "jack ". equals (name) & "root ". equals (psw) {// after successful login, you must pass the user information to the welcome page for request. setAttribute ("name", name); // after successful login, the page is forwarded to the welcome page for request. getRequestDispatcher ("/demo2 "). forward (request, response);} else {request. getRequestDispatcher ("/regist.html "). forward (request, response );}}

2. Write a forwarded Demo2Servlet

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setCharacterEncoding ("UTF-8"); request. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html; charset = UTF-8"); PrintWriter out = response. getWriter (); // get the forwarded data String name = (String) request. getAttribute ("name"); out. println ("Welcome:" + name );}
What is the difference between forwarding and redirection?
1. different request forwarding methods are implemented. getRequestDispatcher ("/regist.html "). forward (request, response); redirect response. sendRedirect ("/day07/regist.html") 2. different requests are forwarded only once. Request redirection is sent twice. 3. the address bar changes. The forwarding address bar does not change. The redirection address bar changes. 4. data can be carried through the request object during forwarding.
Web Path Problems

If you need to use a path in a web project, such as forwarding, redirection, and hyperlinks.

Principle: "All web paths start"

Then, the/path must be a relative path, so/indicates which relative path requires intelligence.

If the path is used by the server,/indicates the root directory of the current website. If the path is used by the browser,/indicates the webapps directory of the current Tomcat.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.