Java struts2 getting started learning instance -- output the Client IP address and access method to the browser

Source: Internet
Author: User

Instance 1: outputs the Client IP address and access method to the browser. IpAction. java View Codeip_struts.xml copy Code <? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name = "ip" namespace = "/" extends = "struts-default"> <action name = "IpAction" class = "com. amos. web. action. ipAction "method =" execute "> </action> </package> </struts> copy the code struts. xml copy Code <? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <! -- Load other configuration files --> <include file = "config/ip_struts.xml"> </include> </struts> copy the code configuration, deploy, run, and view the output. Output result: code analysis: IpAction. java struts2 provides ServletActionContext to encapsulate common request and response objects. Here> 1. Use ServletActionContext. getRequest () gets the request object,> 2. ServletActionContext. getResponse () gets the response object.> 3. Use request. getRemoteAddr () method to obtain the IP address of the access end;> 4. request. the getMethod () method is used to obtain access from the client. The ip_struts.xml package name must be unique and the namespace can be omitted. The result attribute is not used here because return null in IpAction. If retrun "string ", result must be configured; struts. xml does not need to be configured here. You only need to introduce the ip_struts.xml file and use include to introduce the file. The introduced address is a relative path, and whether the file is introduced successfully. Check the prompt information during tomcat startup, or press ctrl to see if the path can be accessed. Example 2: use jsp to output information in instance 1. Method 1: IpAction. java View Codeip_struts.xml to copy the Code <? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name = "ip" namespace = "/" extends = "struts-default"> <action name = "IpAction" class = "com. amos. web. action. ipAction "method =" execute "> <result name =" toJSP "type =" dispatcher ">/ip. jsp </result> </action> </package> </struts> copy the code ip address. jsp is the View Co in the webapp directory. De output result: code analysis: Use the setAttribute method of the request object in IpAction. java to pass the value to the request domain. Request. setAttribute ("clientIP", clientIP); request. setAttribute ("method", method); and return "toJSP" is no longer limited to "success". In fact, as long as the value of name in ip_struts.xml corresponds to it, it is OK. Ip_struts.xml uses the default forwarding method. The value of name is "toJSP", which corresponds to the return value of the execute method in IpAction. java. Then forward the value to the ip address. <result name = "toJSP" type = "dispatcher">/ip in jsp. jsp </result> ip. two methods are used in jsp. The first method is the basic value passing method. The value passing method is successful, and the other method is short, post it here and study it later. TODO: servlet and jsp value transfer problems. Use the getAttribute method of the request object to obtain the value, and then use the basic jsp output method to output the value. Servlet has three domain objects: HttpServletRequest, ServletContext, and HttpSession.

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.