Re: Question in writing Struts Program

2007-06-13 Thread Henry F. Camacho Jr.
Looks to me when you hit your action you are not saving those attributes
in request.

request.setAttribute(username, PersonName);

Then in your JSP you can get the Attribute using getAttribute.

However I don't think you want to do this. Coming out of the action you
should call your business logic to determine if the login was
successful, and it looks like you have the code correct to get the
information from the form. Looking at your code I see the following in
the action:

String PersonName = TestFormBean.getPersonName();
String Psw = TestFormBean.getPsw()

Add something like:

BusinessLogic bl = new BusinessLogic();
try
{
bl.testLogin(PersonName, Psw);
}
catch (BusinessLogicException e)
{
return(mapping.findForward(login failed);
}

return(mapping.findForward(success);

Something like this.  I am pretty new at Struts however.

HFC


友信 徐 wrote:
 Hello,everybody.
Recently,I wrote a simple JSP Web program basing on the Struts 
 architecture.It created a page for a user to input his name and password,and 
 after the user click the submit botton on the form,it can redirect to another 
 page and show the name and password that the use has input on the page.I have 
 completed the program but it can't work properly.
   I listed each part of the program as following:


   (a) TestForm.jsp
   %@ page language=java contentType=text/html; charset=UTF-8  
 pageEncoding=UTF-8%
   !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN

   head
   titleLogin Interface/title
   /head
   body vLink=#00 link=#003366 bgColor=#E0F0F8
   img height=33 src=image/enter.gif width=148 
   form action=ReadTestForm.do method=post
   UserName:
input size=15name=PersonNamep
   Password:
input type=password size=15 name=Pswp
   input type=submit value=Submit
   /form



   (b) MTestForm.java
   package Test;

   import org.apache.struts.action.ActionForm;
   import org.apache.struts.action.ActionMapping;
   import javax.servlet.http.HttpServletRequest;
   import org.apache.struts.action.ActionErrors;
   import org.apache.struts.action.ActionMessage;

   public class MTestForm extends ActionForm{

   private String PersonName = null;
   private String Psw= null;
   public  MTestForm(){}
   
   public void setPersonName(String name) {
   this.PersonName = name;
   }  
   public String getPersonName() {
   return PersonName;
   }  
   public void setPsw(String psw) {
   this.Psw = psw;
   }  
   public String getPsw() {
   return Psw;
   }
   
   public void reset(ActionMapping mapping,
   HttpServletRequest request) {
   this.PersonName = null;
   this.Psw = null;
   }
   
   }



   (c) ReadTestFormAction.java
   package Test;

   import org.apache.struts.action.Action;
   import org.apache.struts.action.ActionForm;
   import org.apache.struts.action.ActionForward;
   import org.apache.struts.action.ActionMapping;

   import org.apache.struts.action.ActionMessages;
   import org.apache.struts.action.ActionMessage;

   import javax.servlet.ServletContext;
   import javax.sql.DataSource;
   import javax.servlet.http.*;

   public final class ReadTestFormAction extends Action{

   public ActionForward execute(
   ActionMapping mapping,
   ActionForm form,
   HttpServletRequest request,  
   HttpServletResponse response) throws Exception {
   
   
MTestForm TestFormBean = (MTestForm) form; 
   String PersonName = TestFormBean.getPersonName();
   String Psw = TestFormBean.getPsw();
   
   
   return mapping.findForward(ReadTestFormOk);
   }
   }



   (d) ShowForm.jsp
   %@ page language=java contentType=text/html; charset=UTF-8
   pageEncoding=UTF-8%
   !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
   %@ page import = classmate.* %
   html
   head
   meta http-equiv=Content-Type content=text/html; charset=UTF-8
   titleRead Test Form and Show the Data/title
   /head
   body
   %
   MTestForm ReadformBean1 = 
 (MTestForm)request.getAttribute(TestFormBean1);
   %
   h1img src=image/smile.gif
   Welcome
   %=ReadformBean1.getPersonName()% 
   Your Password is:
   %=ReadformBean1.getPsw()%

   /h1br
   /body
   /html



   (e) web.xml
   ?xml version=1.0 encoding=ISO-8859-1?

   !DOCTYPE web-app
 PUBLIC -//Sun Microsystems, Inc.//DTD Web Application 2.3//EN
 http://java.sun.com/dtd/web-app_2_3.dtd;

   web-app

 !-- Action Servlet Configuration --
 servlet
   

Question in writing Struts Program

2007-06-12 Thread 友信 徐
Hello,everybody.
   Recently,I wrote a simple JSP Web program basing on the Struts 
architecture.It created a page for a user to input his name and password,and 
after the user click the submit botton on the form,it can redirect to another 
page and show the name and password that the use has input on the page.I have 
completed the program but it can't work properly.
  I listed each part of the program as following:
   
   
  (a) TestForm.jsp
  %@ page language=java contentType=text/html; charset=UTF-8  
pageEncoding=UTF-8%
  !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
   
  head
  titleLogin Interface/title
  /head
  body vLink=#00 link=#003366 bgColor=#E0F0F8
  img height=33 src=image/enter.gif width=148 
  form action=ReadTestForm.do method=post
  UserName:
   input size=15name=PersonNamep
  Password:
   input type=password size=15 name=Pswp
  input type=submit value=Submit
  /form
   
   
   
  (b) MTestForm.java
  package Test;
   
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionMapping;
  import javax.servlet.http.HttpServletRequest;
  import org.apache.struts.action.ActionErrors;
  import org.apache.struts.action.ActionMessage;
   
  public class MTestForm extends ActionForm{
   
  private String PersonName = null;
  private String Psw= null;
  public  MTestForm(){}
  
  public void setPersonName(String name) {
  this.PersonName = name;
  }  
  public String getPersonName() {
  return PersonName;
  }  
  public void setPsw(String psw) {
  this.Psw = psw;
  }  
  public String getPsw() {
  return Psw;
  }
  
  public void reset(ActionMapping mapping,
  HttpServletRequest request) {
  this.PersonName = null;
  this.Psw = null;
  }
  
  }
   
   
   
  (c) ReadTestFormAction.java
  package Test;
   
  import org.apache.struts.action.Action;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
   
  import org.apache.struts.action.ActionMessages;
  import org.apache.struts.action.ActionMessage;
   
  import javax.servlet.ServletContext;
  import javax.sql.DataSource;
  import javax.servlet.http.*;
   
  public final class ReadTestFormAction extends Action{
   
  public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,  
  HttpServletResponse response) throws Exception {
  
  
   MTestForm TestFormBean = (MTestForm) form; 
  String PersonName = TestFormBean.getPersonName();
  String Psw = TestFormBean.getPsw();
  
  
  return mapping.findForward(ReadTestFormOk);
  }
  }
   
   
   
  (d) ShowForm.jsp
  %@ page language=java contentType=text/html; charset=UTF-8
  pageEncoding=UTF-8%
  !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
  %@ page import = classmate.* %
  html
  head
  meta http-equiv=Content-Type content=text/html; charset=UTF-8
  titleRead Test Form and Show the Data/title
  /head
  body
  %
  MTestForm ReadformBean1 = 
(MTestForm)request.getAttribute(TestFormBean1);
  %
  h1img src=image/smile.gif
  Welcome
  %=ReadformBean1.getPersonName()% 
  Your Password is:
  %=ReadformBean1.getPsw()%
   
  /h1br
  /body
  /html
   
   
   
  (e) web.xml
  ?xml version=1.0 encoding=ISO-8859-1?
   
  !DOCTYPE web-app
PUBLIC -//Sun Microsystems, Inc.//DTD Web Application 2.3//EN
http://java.sun.com/dtd/web-app_2_3.dtd;
   
  web-app
   
!-- Action Servlet Configuration --
servlet
  servlet-nameactionServlet/servlet-name
  servlet-classorg.apache.struts.action.ActionServlet/servlet-class
/servlet
   
!-- Action Servlet Mapping --
servlet-mapping
  servlet-nameactionServlet/servlet-name
  url-pattern*.do/url-pattern
/servlet-mapping
   
!-- The Welcome File List --
welcome-file-list
  welcome-fileTestForm.jsp/welcome-file
/welcome-file-list
   
  /web-app
   
   
   
  (f)  struts-config.xml
  ?xml version=1.0 encoding=ISO-8859-1 ?
  !DOCTYPE struts-config PUBLIC 
  -//Apache Software Foundation//DTD Struts Configuration 1.1//EN 
  http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd;
  
  struts-config
   
  form-beans
  form-bean name=formBean1 type=Test.UserForm/
  form-bean   name=TestFormBean1 type=Test.MTestForm
  /form-bean
   
   
  action-mappings
  action path=/login type=Test.LoginAction 
name=formBean1 scope=request input=/login.jsp 
  forward name=failed