1. Trang chủ
  2. » Công Nghệ Thông Tin

Dynamic Web Pages using JSP - Lab Deliverable 9 pps

9 273 1

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 9
Dung lượng 349,2 KB

Nội dung

Introduction to Struts Ver 1.0 © 2005 Aptech Limited 1 Lab Deliverable 9 Introduction to Struts Part II 1. Create a struts-blank.war blank application. Use the blank application to create a Web page that displays the title and author of a book. The page includes a Submit button. After the user clicks the Submit button, a page should appear indicating that the request is being processed. The example requires two JSP pages and two JavaBeans. Update struts-config.xml file to associate the Web pages with the JavaBeans. Solution: The files used in this exercise are: 1. test.jsp 2. searching.jsp 3. BookForm.java 4. BookAction.java <%@ taglib uri = "/WEB-INF/struts-html.tld" prefix="html" %> <html> <head> <title>Struts</title> </head> <body> <html:form action="book.do" ><center> <b>TITLE:<b> <html:text property ="title" /><p> <b>AUTHOR:<b> <html:text property ="author" /><br> <p> <html:submit /></center> </html:form> </body> </html> Enter the code in Notepad and save the file as test.jsp in %TOMCAT_HOME%/webapps/ struts-test. <html> <head><title>Searching</title> </head> <body> <h3><b>Searching <b></h3> </body> </html> Enter the code in Notepad and save the file as searching.jsp in %TOMCAT_HOME%/webapps/ struts-test. package com; import javax.servlet.http.*; 2 Ver 1.0 © 2005 Aptech Limited JSP and Struts import org.apache.struts.action.*; public class BookForm extends ActionForm { private String nTitle = null; private String nAuthor = null; public String getTitle() { return nTitle; } public void setTitle(String aTitle) { nTitle = aTitle; } public String getAuthor() { return nAuthor; } public void setAuthor(String aAuthor) { nAuthor = aAuthor; } public void reset(ActionMapping aMapping, HttpServletRequest aRequest) { nTitle = null; nAuthor = null; } } Enter the Java code in Notepad and save the file as BookForm.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts- test/WEB-INF/classes/com. package com; import javax.servlet.*; import javax.servlet.http.*; import org.apache.struts.action.*; public class BookAction extends Action { public ActionForward perform(ActionMapping aMapping, ActionForm aForm,HttpServletRequest aRequest, HttpServletResponse aResponse)throws ServletException { BookForm f = (BookForm) aForm; String title = f.getTitle(); String author = f.getAuthor(); return aMapping.findForward("saved"); } } Enter the Java code in Notepad and save the file as BookAction.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts- test/WEB-INF/classes/com. Introduction to Struts Ver 1.0 © 2005 Aptech Limited 3 <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> <struts-config> <! Form Bean Definitions > <form-beans> <form-bean name = "bookForm" type = "com.bookForm"/> </form-beans> <! Global Forward Definitions > <global-forwards> <forward name = "start" path = "/index.jsp"/> </global-forwards> <! Action Mapping Definitions > <action-mappings> <action path = "/book" type = "com.bookAction" name = "bookForm"> <forward name = "saved" path = "/greeting.jsp"/> </action> </action-mappings> </struts-config> Update the struts-config.xml file used in the Web application. The output of the program is as shown in Figure 16.1. 4 Ver 1.0 © 2005 Aptech Limited JSP and Struts Figure 16.1: Input page After the user clicks the Submit button, the output of the program is as shown in Figure 16.2. Figure 16.2: Saved page Do It Yourself 1. Create a Login form using struts. Create two text fields in the Web page, and name the fields as User and Password. The page includes a Submit button. After the user clicks the Submit button, a page should appear indicating that the user is a valid user. Solution: Introduction to Struts Ver 1.0 © 2005 Aptech Limited 5 The files used in this exercise are: 1. cust.jsp 2. valid.jsp 3. CustForm.java 4. CustAction.java <%@ taglib uri = "/WEB-INF/struts-html.tld" prefix="html" %> <html> <head> <title>Testing struts</title> </head> <body> <html:form action = "cust.do" > <center> <b>User:</b>&nbsp;&nbsp;&nbsp;<html:text property = "first" /> <br><br><br> <b>Password:</b><html:password property = "pwd" /><br><br><br> <html:submit /> </center> </html:form> </body> </html> Enter the code in Notepad and save the file as cust.jsp in %TOMCAT_HOME%/webapps/ struts-test. <html> <head><title>Valid User</title> </head> <body> <center><h2><b>VALID USER</b></h2></center> </body> </html> Enter the code in Notepad and save the file as valid.jsp in %TOMCAT_HOME%/webapps/ struts-test. package common.test; import javax.servlet.http.*; import org.apache.struts.action.*; public class CustForm extends ActionForm { private String mFirst = null; private String mPwd = null; public String getFirst() { return mFirst; } public void setFirst(String aFirst) { mFirst = aFirst; } public String getPwd() { return mPwd; } public void setPwd(String aPwd) { mPwd = aPwd; } 6 Ver 1.0 © 2005 Aptech Limited JSP and Struts public void reset(ActionMapping aMapping, HttpServletRequest aRequest) { mFirst = null; mPwd = null; } } Enter the Java code in Notepad and save the file as CustForm.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts- test/WEB-INF/classes/common/test. package common.test; import javax.servlet.*; import javax.servlet.http.*; import org.apache.struts.action.*; public class CustAction extends Action { public ActionForward perform(ActionMapping aMapping, ActionForm aForm,HttpServletRequest aRequest, HttpServletResponse aResponse)throws ServletException { CustForm f = (CustForm) aForm; String first = f.getFirst(); String last = f.getPwd(); return aMapping.findForward("saved"); } } Enter the Java code in Notepad and save the file as CustAction.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts- test/WEB-INF/classes/common/test. <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> <struts-config> <! Form Bean Definitions > <form-beans> <form-bean name = "custForm" type = "common.test.CustForm"/> Introduction to Struts Ver 1.0 © 2005 Aptech Limited 7 </form-beans> <! Global Forward Definitions > <global-forwards> <forward name = "start" path = "/index.jsp"/> </global-forwards> <! Action Mapping Definitions > <action-mappings> <action path = "/cust" type = "common.test.CustAction" name = "custForm"> <forward name = "saved" path = "/valid.jsp"/> </action> </action-mappings> </struts-config> Update the struts-config.xml file used in the Web application The output of the program is as shown in Figure 16.3. Figure 16.3: Login page After the user clicks the Submit button, the output of the program is as shown in Figure 16.2. 8 Ver 1.0 © 2005 Aptech Limited JSP and Struts Figure 16.4: Valid user page . %TOMCAT_HOME%/webapps/struts- test /WEB- INF/classes/common/test. <?xml version="1.0" encoding="ISO-885 9- 1 " ?> <!DOCTYPE struts-config PUBLIC " ;-/ /Apache Software. in %TOMCAT_HOME%/webapps/struts- test /WEB- INF/classes/com. Introduction to Struts Ver 1.0 © 2005 Aptech Limited 3 <?xml version="1.0" encoding="ISO-885 9- 1 " ?>. two JSP pages and two JavaBeans. Update struts-config.xml file to associate the Web pages with the JavaBeans. Solution: The files used in this exercise are: 1. test .jsp 2. searching.jsp

Ngày đăng: 14/08/2014, 05:20

TỪ KHÓA LIÊN QUAN