Home > Default > How to retrieve data and display in JSP page

How to retrieve data and display in JSP page

October 11Hits:0
Advertisement
hi,
i am trying to retrieve data from SQL server 2000 and display in JSP Page. I have already place the codes of the retrieve in the bean file. I wanna ask is that how to display in the JSP page. If possible, can provide example codings for mi to reference?
Thanks
Regards,
shixuan

Answers

HI Tan ,
I pressume that you wanted to make use of PDK, the code can go like this .
<b><u>1) JAVA file</u></b>
import com.sapportals.htmlb.DropdownListBox;
     import com.sapportals.htmlb.InputField;
     import com.sapportals.htmlb.event.Event;
     import com.sapportals.htmlb.page.DynPage;
     import com.sapportals.htmlb.page.PageException;
     import com.sapportals.portal.htmlb.page.JSPDynPage;
     import com.sapportals.portal.htmlb.page.PageProcessorComponent;
     import com.sapportals.portal.prt.component.IPortalComponentRequest;
     import com.sapportals.portal.prt.component.IPortalComponentSession;
     import com.sapportals.portal.prt.component.IPortalComponentContext;
     import java.sql.*;
     public class P_SAP_B_User extends PageProcessorComponent
     * Method          :           getPage()
     * Description      :                         
     * Input Parameters     :     None
     * Returns          :          Object of Class DynPage     
          public DynPage getPage()
              return new P_SAP_B_UserDynPage();
            }     // end of dynPage()
            public static class P_SAP_B_UserDynPage extends JSPDynPage
              /* Variable Declaration     */
               /* Object of bean class P_SAP_B_CreateUser initialised to null */
                   private P_SAP_B_CreateUser createUserBean = null;
              /* Flags for checking the occurance of Event & Error. */
              private int iFlag=0;
              private int iErrFlag=0;
              /* Variables for storing the information
                      entered by user in each text field */
              private String sFname;
              private String sSname;
              private String sAge;
              private String sExp;
              private String sSkill;
              private String sUnit;
     * Method          :           doInitialization()
     * Description      :                         
     * Input Parameters     :     None
     * Returns          :          None
              public void doInitialization()
                     IPortalComponentSession componentSession = ((IPortalComponentRequest)getRequest()).getComponentSession();
                     Object o = componentSession.getValue("createUserBean");
                     if(o==null || !(o instanceof P_SAP_B_CreateUser))
                       createUserBean = new P_SAP_B_CreateUser();
                       componentSession.putValue("createUserBean",createUserBean);
                    }     // end of if
                     else
                         createUserBean = (P_SAP_B_CreateUser) o;
                     }     // end of else
               }//end of doInitialisation()
     * Method          :           onUpdate()
     * Description      :                         
     * Input Parameters     :     object of Event class
     * Returns          :          None
               public void onUpdate(Event e)throws PageException
                    /*     sets flag to 1 when update button is clicked. */
                    iFlag=1;
     * Method          :           doProcessAfterInput()
     * Description      :                         
     * Input Parameters     :     None
     * Returns          :          None
              public void doProcessAfterInput() throws PageException
                         InputField ifFirstName = (InputField) getComponentByName("FirstName");
                         InputField ifSecondName = (InputField) getComponentByName("SecondName");
                         InputField ifAge = (InputField) getComponentByName("Age");
                         InputField ifExp = (InputField) getComponentByName("Exp");
                         InputField ifSkill = (InputField) getComponentByName("Skill");
                         DropdownListBox dlbUnit = (DropdownListBox) getComponentByName("Unit");
                         int iAge,iExp;
                         IPortalComponentRequest request = (IPortalComponentRequest) this.getRequest();
                        IPortalComponentContext myContext = request.getComponentContext();
                         P_SAP_B_CreateUser myNameContainer = (P_SAP_B_CreateUser) myContext.getValue("createUserBean");
                         if(ifFirstName != null)
                              this.sFname = ifFirstName.getValueAsDataType().toString() ;
                         }     // end of if
                         if(ifSecondName!= null)
                              this.sSname = ifSecondName.getValueAsDataType().toString() ;
                         }      // end of if
                         if(ifAge!= null)
                              this.sAge = ifAge.getValueAsDataType().toString() ;
                         }     // end of if
                         if(ifExp!= null)
                              this.sExp = ifExp.getValueAsDataType().toString() ; 
                         }     // end of if                         
                         if(ifSkill != null)
                              this.sSkill = ifSkill.getValueAsDataType().toString() ;          
                         }     // end of if
                         if(dlbUnit != null)
                              this.sUnit = dlbUnit.getSelection().toString() ;     ;
                         }      // end of if
                      /* Data Validation */
                         /* try block for numeric Exception */
                         try
                             /* checking for any field left blank by the user */
                              if(sFname.equals("") || sSname.equals("") ||  sAge.equals("")|| sExp.equals("") || sSkill.equals(""))
                                    /* set error flag to 1 in case of any field left blank */
                                    iErrFlag=1;
                              } // end of if
                              else
                                    /* converting Age and Experience fields (String) to integer */
                                     iAge= Integer.parseInt(sAge);
                                     iExp= Integer.parseInt(sExp);
                                   /* setting the boundaries on the value in Age Field */
                                     if(iAge<0)
                                         /* set error flag to 2 in case of age below 0 */
                                         iErrFlag=2;
                                     }// end of if
                                   /* setting the boundaries on the value in Experience field */
                                      else if(iExp<0 ||(iExp/12)>=iAge)
                                          /* set error flag to 3 in case of experience below 0 or exceeding the age in years */
                                          iErrFlag=3;
                                      }// end of else if
                                      /* In case of no error */
                                      else
                                         /* setting the bean variables */
                                         try
                                             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                             Connection con = DriverManager.getConnection("jdbc:odbc:Test");
                                             String query="insert into UserData values(?,?,?,?,?,?)";
                                             PreparedStatement prestat=con.prepareStatement(query);
                                             /* setting the values to be inserted into the user table */
                                             prestat.setString(1,sFname);
                                             prestat.setString(2,sSname);
                                             prestat.setString(3,sAge);
                                             prestat.setString(4,sExp);
                                             prestat.setString(5,sSkill );
                                             prestat.setString(6,sUnit);
                                             prestat.executeUpdate() ;
                                             prestat.close();
                                             con.close();
                                             myNameContainer.setSFname(sFname);
                                             myNameContainer.setSLname(sSname);
                                             myNameContainer.setSAge(sAge);
                                             myNameContainer.setSExp(sExp);
                                            myNameContainer.setSSkill(sSkill);
                                            myNameContainer.setSUnit(sUnit);
                                         } // end of inner try block
                                          catch(Exception sqle)
                                             myNameContainer.setErrMessage("Update failed ! Please try again." );
                                          } // end of catch corresponding to inner try
                                                       } // end of inner else
                                                  } //end of outer else
                               } //end of outer try block
                               catch(Exception e)
                                 /* setting flag to 4 in case of non-numeric age/experience values */
                                 iErrFlag = 4;
                                 /* Displaying error message corresponding to the value of error flag */
                              switch(iErrFlag)
                                /* Empty Field */
                                case 1:myNameContainer.setErrMessage( "Please Fill all the fields");
                                break;
                                /* Invalid Value in the age field */
                                case 2:myNameContainer.setErrMessage( "Enter a valid value in Age field.(Hint : Have you entered Age<0 ?");
                                break;
                                /* Invalid Value in the experience field */
                                case 3:myNameContainer.setErrMessage( "Enter a valid value in Experience field.(Hint : Experince should not be negative or greater than your age in months)");
                                break;
                                /* Non-numeric value in the Age/ experience fields */
                                case 4:myNameContainer.setErrMessage( "Please Enter Numeric Value for Age and Experience");
                                break;
                             } // end of switch-case block
          } //end of doProcessAfterInput()
     * Method          :           doProcessBeforeOutput()
     * Description      :                         
     * Input Parameters     :     None
     * Returns          :          None
              public void doProcessBeforeOutput() throws PageException
                   /* Displays Form for new user creation by default */
                   this.setJspName("P_SAP_B_UserCreationForm.jsp");
                     /* In case of an error display an error message page */
                     if(iErrFlag!=0)
                          setJspName("ErrorPage.jsp");
                     } //end of if
                     /* Displays the user's information as entered in the SQL
                        database after its been uploaded by the user */
                     else if(iFlag==1)
                         setJspName("hello.jsp");      
                      } // end of else if
              } // end of doProcessBeforeOutput()
          } // end of P_SAP_B_UserDynPage Class
     } // end of P_SAP_B_User class
* End of File P_SAP_B_User.java
2) Bean
package com.sap.usercreation;
import java.io.Serializable;
public class P_SAP_B_CreateUser implements Serializable
     private String sFname;
     private String sLname;
     private String sUnit;
     private String sSkill;
     private String sExp;
     private String sAge;
     private String errMessage;
     * @return
     public String getSFname() {
          return sFname;
* @return
public String getSLname() {
     return sLname;
* @param i
* @param string
public void setSFname(String string) {
     sFname = string;
* @param string
public void setSLname(String string) {
     sLname = string;
* @return
public String getSUnit() {
     return sUnit;
* @param string
public void setSUnit(String string) {
     sUnit = string;
* @return
public String getSSkill() {
     return sSkill;
public void setSSkill(String string) {
     sSkill = string;
* @return
public String getSAge() {
     return sAge;
* @return
public String getSExp() {
     return sExp;
* @param string
public void setSAge(String string) {
     sAge = string;
* @param string
public void setSExp(String string) {
     sExp = string;
* @return
public String getErrMessage() {
     return errMessage;
* @param string
public void setErrMessage(String string) {
     errMessage = string;
3) The Jsp file i have already posted.
See if you copy this code and paste it wont work as i have not given you full code ,But yes this gives you an overview of how things can be done .
Thanx
Pankaj

Read other 8 answers

Tags:

Related Articles

  • How to retrieve data and display in JSP pageOctober 11

    hi, i am trying to retrieve data from SQL server 2000 and display in JSP Page. I have already place the codes of the retrieve in the bean file. I wanna ask is that how to display in the JSP page. If possible, can provide example codings for mi to ref

  • Getting image path from database( Remote server)  & display in jsp pageOctober 11

    hai frnds, i have one doudt regarding in my web application Development . I want to Getting image path from database( **Remote server**) & display in jsp page here iam forwarding my control through Servlets how this will passiable Thanks in Advance S

  • Pass the data back from the jsp page to the java codeOctober 11

    Hi, I have written an iView that receives an event using EPCF and extracts data from the client data bag. I need this iView to pass the data back from the jsp page to the java code. I am trying to do this using a hidden input field, but I cannot get

  • Displaying a jsp page content as a pdf...November 30

    HI, I am writing a simple jsp page.When I request that jsp page I want to display it in a pdf format in the browser. I have given the content type as "application/pdf", but still its showing as a normal web page. But when I am making the content

  • How To Customize displaying  of JSP page From Backing BeanNovember 30

    I would like to customize the displaying of jsp page from backing bean based on user Roles For example - if the Role is Admin then backing bean should display Admin.jsp - if the Role is Guest then backing bean should display Guest.jsp Any pointers/su

  • How to display retrieved data from DB on web pageNovember 30

    Dear all, how to display retrieved data from oracle database on web page,Does anyone can give me an simple example? thanks WJHi, I use JRun tags but you can use Oracle tags or just JDBC... First u need to install jdbs connection and set e connection

  • Cant display index.jsp page of application folderNovember 30

    Hello experts, I am trying to do one simple struts application using Struts2 get the data and display it.. I kept my folder that is Struts2Application in webbaps dir of tomcat.. when I write the "http://localhost:8080/Struts2Application" it disp

  • Database connection and display in JSP pageOctober 11

    hi, I am new to SAP netweaver. I have created a project using JSPDynPage, Beans and java file. I have configure the datasouce in Visual Composer. I deploy the project on the portal itself. I have try all kinds of codes bt cannot retrieve. Is there an

  • Insert data into table from JSP page using Entity Beans(EJB 3.0)October 11

    I want to insert data into a database table from JSP page using Entity Beans(EJB 3.0). 1. I have a table 'FRIENDS', (in Oracle 10g database). 2. It has two columns, 'NAME' and 'CITY'. Both have datatype strings(varchar2). 3. Now from a JSP page, havi

  • Pictures are not displaying in jsp pageOctober 11

    hi everyone, i want to display background images in my jsp page also some other images which are not background images plz tell me the way that enables me to place images in my jsp page thanx<body background="pic/love.gif"> <% --- jsp c

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 0.678 s.