Hello world of Tomcat + Struts

Source: Internet
Author: User

1. install Tomcat

2. Download struts Full distribution to the http://struts.apache.org/download.cgi#struts1310

3. decompress the downloaded file.

4. there is the apps folder in the folder decompressed above, there is a struts-blank-1.3.10.war file in the folder, decompress the file to the struts-hello-world folder (you do not need to create the struts-hello-world folder in advance)

Unzip struts-blank-1.3.10.war-D Struts-Hello-world 

5. Create a New regSuccess. jsp in struts-hello-world. The content is as follows:

<Html> <br/> <pead> <br/> <title> <br/> User Registration Was Successful! <Br/> </title> <br/> </pead> <br/> <body> <br/> <p> User Registration Was Successful! </H1> <br/> </body> <br/> </ptml> 

6. Create a strutsTutorial folder under struts-hello-world/WEB-INF/src/java, and then create a UserRegistrationAction. java file in it, the content is as follows:

Package strutsTutorial; <br/> import javax. servlet. http. *; <br/> import org. apache. struts. action. *; <br/> public class UserRegistrationAction extends Action {<br/> public ActionForward execute (<br/> ActionMapping mapping, <br/> ActionForm form, <br/> HttpServletRequest request, <br/> HttpServletResponse response) <br/> throws Exception {</p> <p> return mapping. findForward ("success"); <br/>}< br/>} 

7. Compile the java source file above. You must specify two jar files during compilation. One is the servlet-api.jar, located at $ CATALINA_HOME/common/lib; the other is the struts-core-1.3.10.jar, located at struts-hello-world/WEB-INF/lib. In addition, you must specify the storage location of the compiled class file (of course, you can also manually copy it after compilation ). The command line used in the two steps is as follows (do not be afraid of it, but the path of the two jar files is long ):

Javac-classpath "C:/Program Files/Apache Software Foundation/Tomcat 5.5/common/lib/servlet-api.jar; G: /xkf's documents/Downloads/struts-1.3.10-all (1) /struts-1.3.10/apps/struts-hello-world/WEB-INF/lib/<br/> struts-core-1.3.10.jar "-d .. /.. /.. /classes UserRegistrationAction. java 

After the execution, you can find the compiled class file in struts-hello-world/WEB-INF/classes/strutsTutorial

8. Modify struts-hello-world/WEB-INF/struts-config.xml

Open the file in notepad, find </Action-mappings>, and add

<Action path = "/userRegistration" type = "strutsTutorial. userRegistrationAction "> <br/> <forward name =" success "path ="/regSuccess. jsp "/> <br/> </action> 

9. In the last step, copy Struts-Hello-world to $ catalina_home/webapps, restart the Tomcat server, and open the link http: // localhost: 8080/Struts-Hello-World/userregistration. Do

 

Stir up and eat bowl noodles

 

Principles:

Several lines of key configuration in Web. XML in Struts-Hello-World/WEB-INF

<Servlet> <br/> <servlet-Name> action </servlet-Name> <br/> <servlet-class> Org. apache. struts. action. actionservlet </servlet-class> <br/> <init-param> <br/> <param-Name> config </param-Name> <br/> <param-Value >/WEB-INF/struts-config.xml </param-value> <br/> </init-param> <br/> <load-on-startup> 2 </load-on-startup> <br/> </servlet> <br/> <! -- Standard action Servlet Mapping --> <br/> <servlet-mapping> <br/> <servlet-Name> action </servlet-Name> <br/> <URL-Pattern> *. DO </url-pattern> <br/> </servlet-mapping> 

The struts-config.xml configuration file is specified in the <servlet> element, and the <servlet-mapping> specifies that all URL requests ending with. Do are processed by the Action servlet. Action this servlet looks at the struts-config.xml and it sees

<Action Path = "/userregistration" type = "strutstutorial. userregistrationaction "> <br/> <forward name =" success "Path ="/regsuccess. JSP "/> <br/> </Action> 

Http: // localhost: 8080/Struts-Hello-World/userregistration is displayed. at the end of DO is userregistration. do, then generate strutstutorial. an object of userregistrationaction that calls its execute method. This method returns an actionforward called success, successactionforward is the <forward name = "success" Path = "/regsuccess in the configuration file. JSP "/>, so/regsuccess. JSP is returned to the browser, and the processing is complete.

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.