Monday, May 24, 2010

Strut2-String Validation


Struts 2 – String Validation


Aim:

            It is very important one to validate user input when we are going to create the web project. So we will learn how to validate the user input such as String , integer, Email etc,.

Procedure:

            Step_1:

                        Create ActionSupport

                       Create Folder example under source packages(if it dose not).

                       If it is, we use already created Folder example.

                        Right click on example

New Other under categories Struts2 and under File types Struts2 Action.

Next where you give class name and click Finish.


Include following coding into the action support

package example;
import com.opensymphony.xwork2.ActionSupport;

public class StringVaLidationAction extends ActionSupport{

  private String username;

  public String execute() throws Exception
  {
    if (getUsername() != null)
    {
      return SUCCESS;
    }
    else
    {
      return ERROR;
    }
  }

  public void setUsername(String username)
  {
    this.username = username;
  }
  public String getUsername()
  {
    return username;
  }
}



Step_2:

Create jsp file

Create Folder example under web pages

Right click on example . New other web jsp you give jsp name ( I have used jsp name as stringInputForm.jsp.

< %@ taglib prefix="s" uri="/struts-tags" % >

< html >

< head >
< title > Input form < /title >
< s:head/ >
< /head >

< body >

< s:form method="POST" action="stringValidation" >

< s:textfield label="Enter User Number" name="username" maxlength="10" / >

< s:submit / >
< /s:form >

< /body >

< /html >


Step_3:

Create stringSuccess.jsp

Include following code into the string Success.jsp

< %@page language="java" %>
< html >

< head >
< title > Correct entry < /title >
< /head >

< body >
< b > Welcome to < /b > < %=request.getParameter("username") % > !
< /body >

< /html >


Step_4:

Made come changes in struts.xml.

Source package
|
|
< default package >
|
|
struts.xml

< ?xml version="1.0" encoding="UTF-8" ? >
< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd >

< struts >

< package name="example" namespace="/example" extends="struts-default" >

< action name="stringValidation" class="example.StringVaLidationAction" >

< result name="input" > /example/stringInputForm.jsp < /result >
< result name="error" > /example/stringInputForm.jsp < /result >
< result name="success" > /example/stringSuccess.jsp < /result >
< /action >

< /package >

< /struts >









Step_5:


Structure of the application


Step_6:

Run stringInputForm.jsp

Right click on stringInputForm.jsp

Conclusion:


From this post we have learnt how to validate String input. You have any doubt, you will post comments.


Saturday, May 22, 2010

Struts 2 - Login Application


Struts2 Application-Login page Application
Aim:
          To develop login application using struts2 framework.

Software Requirements:
           A). NetBean 6.8
                 www.netbeans.org/

                B). Download plugin for struts2  
                http://plugins.netbeans.org/PluginPortal/

Requirement:
              1). HelloWorld.jsp
             2). Success.jsp
             3). Failure.jsp
             4). Made some changes in example.xml
             5). Include example.xml into struts.xml
             6). Create Helloworld.java


Procedure:

        Step_1: Open NetBean 6.8



Step_2: Goto Fileà New Project. The New Project window will open. Where you select  under categories Java Web and under Projects web Application . And Click Next. The New Web Application window will open. Where you give project name ( I have given project name as LoginproA). Click Next and you select Apache server and click Next and the New web Application window will open. Where you select checkbox for struts 2. Click Finish. Now you have successfully created the struts project.

            Step_3: Structure of the project after creating 






Step_4: Creating jsp file

Web pages
|
|
example
|
|
*.jsp file



Step_4.1:Create HelloWorld.jsp

    Right click on example under web pages.
    Newàotherà under categories web – under File types jsp
    Give jsp file name as HelloWorld.jsp
    Include following coding into HelloWorld.jsp

< %@ page contentType="text/html; charset=UTF-8" % >

< %@ taglib prefix="s" uri="/struts-tags" % >

< html >
< head >
< title > Login Application < /title >
< /head >
< body >
< s:form action="login" >
< s:textfield name="username" label="UserName"/ > < br >
< s:password name="password" label="PassWord"/ > < br >
< s:submit value="Login" label="Login"/ >
< /s:form >
< /body >
< /html >

Step_4.2: Create success.jsp

< %@ taglib prefix="s" uri="/struts-tags" % >

< html >
< head >
< title > Login Application < /title >
< /head >
< body >
< h2 > Success < /h2 >
< /body >
< /html >


Step_4.3: Create failure.jsp



< %@ taglib prefix="s" uri="/struts-tags" % >

< html >
< head >
< title > Login Application < /title >
< /head >
< body >
< h2 > Failure < /h2 >
< /body >
< /html >


Step_5: Creating action file

                                Source Package
                                                |
                                                |
                                         example
                                                |
                                                |
                                        *.java
Step_5.1:Create HelloWorld.java under source package
Include following coding
        
package example;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport
{
    private String username;
    private String password;
     public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String execute() throws Exception
    {
        if(getUsername().equals("suresh")&&(getPassword().equals("kumar")))
        {
            return "success";
        }
        else
        {
            return "failure";
        }
    }
 }

Step_6: Configuration file

           There are two configuration file
           One is example.xml
           Another one is struts.xml

Source Package
|
|
< default-package >
|
|
example.xml
struts.xml

example.xml

include following code into example.xml


…….
…….

< struts >
< package name="example" namespace="/example" extends="struts-default" >
< action name="login" class="example.HelloWorld" >
< result > /example/HelloWorld.jsp < /result >
< result name="success" > /example/success.jsp < /result >
< result name="failure" > /example/failure.jsp < /result >
< /action >
< /package >
< /struts >


struts.xml

include following coding into struts.xml

……
……

< struts >
< include file="example.xml"/ >
< !-- Configuration for the default package. -- >
< package name="default" extends="struts-default" >
< /package >
</struts >


Step_7:

Run HelloWorld.jsp à right click on HelloWorld.jspà run file

Conclusion:

From this post, how to create structure and how to create jsp file and class file .

How to run the struts2 application.You have any doubt give comments.


Thursday, May 20, 2010

Struts Application-Logic Tag II

Later

Struts Application-Logic Tag II

Later

Struts Application- Logic Tag Example

Struts Application- Logic Tag Example

Aim:

To perform logical operation in struts application using Logic tag.

< logic:empty >
< logic:notempty >
< logic:match >
< logic:notmatch >

All above mentioned tag have some common attributes name property.

But < logic:match > and < logic:notmatch > have additional attributes that is value.

Procedure:

Step_1:

Create actionforms(I have used actionforms name as logicforms).

Step_2:

Create actions(I have used actin name as logicactions).

Step_3:

Create jsp files

A). Home.jsp

B). success.jsp

Step_4:

Made changes in struts-config.xml

Step_5:

Run Home.jsp

Explain:

Step_1:

Create actionforms

Already I have explained how to create actionforms. You follow that rules.

package actionforms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class logicforms extends org.apache.struts.action.ActionForm
{

private String name;

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

}


Step_2:

Create action

I have already explained how to create action.

package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class logicactions extends org.apache.struts.action.Action
{


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{

return mapping.findForward("success");
}
}

Step_3:

Create jsp files.

A) Home.jsp

< %@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< %@taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >

< html >
< head >

< title > JSP Page < /title >
< /head >
< body >

< html:form action="/logic" >

< html:text property="name"/ >

< html:submit value="OK"/ >

< /html:form >

< /body >

< /html >


B). success.jsp

< %@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< %@taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >

< html >
< head >

< title > JSP Page < /title >
< /head >
< body >

< logic:empty property="name" name="logicforms" >
I am in logic:empty Tag.
< /logic:empty >
< br >
< logic:notEmpty property="name"name="logicforms" >
I am in logic:notEmpty Tag.
< /logic:notEmpty >
< br >
< logic:match property="name" value="suresh" name="logicforms" >
Hello Suresh
< /logic:match >
< br >
< logic:notMatch name="logicforms" property="name" value="sures" >
Hello! da ! You aer not match
< /logic:notMatch >
< /body >
< /html >


Step_4:

Made changes in struts-config.xml

< action-mappings >

< action input="/" name="logicforms" path="/logic" scope="session" type="actions.logicactions" >
< forward name="success" path="/jsp/success.jsp"/ >
< /action >

< /action-mappings >


Step_5:

Run Home.jsp

Conclusion:

From this tag we have learnt the logic:empty,logic:notempty , logic:match, logic:notmatch.

You have any doubt please post comment. I clarify your doubt.

Monday, May 17, 2010

Struts Application-Bean Tag Example-I

Struts Application-Bean Tag Example

Aim:

The < bean:write > tag gets the value of a named bean property. This tag uses the attributes, namely, name, property, and scope. The value specified by the attributes will be output to the JSPWriter

The < bean:message > tag gets value from ApplicationResource.properties.


Procedure:

            Step_1: Create actionforms

            Step_2: Create actions

            Step_3: Create jsp file

            Step_4: Change in struts-config.xml file

            Step_5: Run the file

Step_1:

            Create actionforms (BeanForms)

            Include following code


package actionforms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;


public class BeanForms extends org.apache.struts.action.ActionForm
{
   
    private String name;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

}


Step_2:

            Create action(BeanActions)

Include following coding

package actions;

import actionforms.BeanForms;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class BeanActions extends org.apache.struts.action.Action
{
   
  
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        BeanForms sform=(BeanForms)form;
      
        return mapping.findForward("bean");
    }
}

Step_3:

Create Input.jsp file

Include following coding

< %@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >

< html >
< head >

< title > JSP Page For Input < /title >
< /head >
< body >

< html:form action="/bean" >
< bean:message key=”struts.name” > < html:text property="name"/ >
< html:submit value="OK"/ >
< /html:form >
< /body >
< /html >

Create Output.jsp file

Include following code

< %@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >

< html >
< head >

< title > JSP Page For Input < /title >
< /head >
< body >

< bean:write property="name" name="BeanForms"/ >
< /body >
< /html >

Step_4:

Made change in struts-config.xml

……..

< action-mappings >
< action input="/" name="BeanForms" path="/bean" scope="session" type="actions.BeanActions" >
< forward name="bean" path="/jsp/Output.jsp"/ >
< /action >

< /action-mappings >

…….

Step_5:

Include struts.name=Name in ApplicationResource.properties



Step_6:

Run Input.jsp

Conclusion:

From this,we can understand how to write the output using < bean:write > tag in struts framework.


Saturday, May 15, 2010

Struts Application-HTML-II


 Struts Application- HTML TAG EXAMPLE


Aim:

< html:textArea > , < html:checkbox >

< html:checkbox > is used as to select checkbox same as used in Html.

< html:textarea > is same as in used in html.


Procedure:

            Step_1: Create Actionforms

            Step_2: Create Actions

            Step_3: Configure struts-config.xml

            Step_4: Create Jsp file


Step_1:

            Create Actionforms ( I have used as checkboxform)

            Include following code:

package actionforms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class checkboxform extends org.apache.struts.action.ActionForm
{
   
    private String checkbox;
    private String textarea;

    public String getTextarea()
    {
        return textarea;
    }

    public void setTextarea(String textarea)
    {
        this.textarea = textarea;
    }

    public String getCheckbox()
    {
        return checkbox;
    }

    public void setCheckbox(String checkbox)
    {
        this.checkbox = checkbox;
    }
   
}


Step_2:

            Create Action class ( I have used as checkAction)

Include following code

package actions;

import actionforms.checkboxform;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class checkAction extends org.apache.struts.action.Action
{
   
   
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        checkboxform sform=(checkboxform)form;
        String s=sform.getCheckbox();
        String msg=sform.getTextarea();

        Vector v=new Vector();

        v.add(s);;
        v.add(msg);
        request.setAttribute("msg", v);

        return mapping.findForward("success");
    }
}



Step_3:

Made changes in struts-config.xml


…….
…….

< form-beans >

< form-bean name="checkboxform" type="actionforms.checkboxform"/ >

< /form-beans >

…….
…….

< action-mappings >
< action input="/" name="checkboxform" path="/check" scope="session" type="actions.checkAction" >
< forward name="success" path="/jsp/checkbox.jsp"/ >
< /action >
< /action-mappings >

……..
……..

Step_4:

Create home.jsp


< %@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< html >
< head >

< title > JSP Page < /title >
< /head >
< body >
< html:form action="/check" >
Reading Books < html:checkbox property="checkbox" value="Reading"/ >
Address < html:textarea property="textarea"/ >
< html:submit value="ok"/ >
< /html:form >
< /body >
< /html >



checkbox.jsp

< %@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >

< %@page contentType="text/html" import="java.util.*" % >

< html >
< head >

< title > JSP Page < /title >
< /head >
< body >
< %
Vector msg=(Vector)request.getAttribute("msg");
for(int i=0;i &lt msg.size(); i++)
{
out.println(msg.get(i));
}
% >
< /body >
< /html >




Step_5:

            Run home.jsp

Thursday, May 13, 2010

Struts Application-Html-I



                                           Struts Application-Html Tag Example


AIM:

HTML Tag is used in Struts Framework. This Tag include

< html:text >

< html:password >

< html:radio >


Procedure:
               Step_1:     
                   Create actionforms named  HtmlForms
package actionforms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class HtmlForms extends org.apache.struts.action.ActionForm
{
   private String fname;
   private String lname;
   private String username;
   private String password;
   private String sex;
    public String getFname()
   {
        return fname;
    }
    public void setFname(String fname)
   {
        this.fname = fname;
    }
    public String getLname()
   {
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }
    public String getPassword()
   {
        return password;
    }
    public void setPassword(String password)
   {
        this.password = password;
    }
    public String getSex()
   {
        return sex;
    }
    public void setSex(String sex)
   {
        this.sex = sex;
    }
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
}

Step_2:
Create Action HtmlActions
package actions;
import actionforms.HtmlForms;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class HtmlActions extends org.apache.struts.action.Action
{
     public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        HtmlForms sform=(HtmlForms)form;
        String fname=sform.getFname();
        String lname=sform.getLname();
        String uname=sform.getUsername();
        String pwd=sform.getPassword();
        Vector v=new Vector();
        v.add(fname);
        v.add(lname);
        v.add(uname);
        v.add(pwd);
        v.add(sex);
        request.setAttribute("html",v);
        return mapping.findForward("success");
    }
}

Step_3:
Configure Struts-config.xml
………
< form-beans >
< form-bean name="HtmlForms" type="actionforms.HtmlForms"/ >
< /form-beans >
………
< action-mappings >
………
< action
input="/"
name="HtmlForms"
path="/html"
scope="session"
type="actions.HtmlActions" >
< forward name="success" path="/jsp/success.jsp"/ >
< /action >
………

Step_4:
Creating HtmlForm.jsp
< %@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< html > < head > < title > Html Form < /title > < /head >
< body >
< center > Example For HTML < /center >
< pre >
< html:form action="/html" >
First_Name: < html:text property="fname"/ >
Last_Name: < html:text property="lname"/ >
UserName: < html:text property="username"/ >
Password: < html:password property="password"/ >
Sex:Male < html:radio property="sex" value="Male"/ > FeMale
< html:radio property="sex" value="Female"/ >
< html:submit value="OK"/ >
< /html:form >
< /pre >
< /body >
< /html >

Create success.jsp
< %@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< %@page import="java.util.*" % >

< html >
< head >
< title > Html Tag Example < /title >
< /head >
< body >
< %

Vector v=(Vector)request.getAttribute("html");

out.println("First_name: "+v.get(0));
% >
< br >
< %
out.println(" ");
out.println("Last_name: "+v.get(1));
% >
< br >
< %
out.println(" ");
out.println("User_name: "+v.get(2));
% >
< br >
< %
out.println(" ");
out.println("Password: "+v.get(3));
% >
< br >
< %
out.println("");
out.println("sex: "+v.get(4));
% >

< /body >
< /html >

Tuesday, May 11, 2010

Struts Application- JDBC-Select


Struts Application-Jdbc Select

Aim:

This post will show you how to retrieve the data from database( I have used database mysql).

Procedure:

            Step_1:
                        Create Database ( I have used NStudent).

                        Already I have explained how to create database in previous post

            Step_2:
                        Create Table ( I have used NStudent).

                        Already I have Explained how to create table in previous post.

            Step_3:
                        Create Project using netbean.(I have used project name as JdbcSelectPro).

            Step_4:
                        Create Folder ( under source packages)

                        a). actionforms

                        b). actions

                        c). bl

                        d). vo

                        e). Dbcon

            Step_5:
                        Create java class for database connection.

                        I have used dbcon as java class.

                        Include following coding in dbcon.java
                       

                        package Dbcon;
                        import java.sql.*;
                        public class dbcon
                        {
                               public static Connection getConnection()
                               {
                                      Connection con=null;
                                      Statement stmt=null;
                                      ResultSet rs=null;
       
                                      try
                                      {
                                              Class.forName("com.mysql.jdbc.Driver");
                                       }
                                       catch(Exception e)
                                       {
                                             System.out.println("Driver Load Driver");
                                       }
                                       try
                                       {
                                             String url="jdbc:mysql://localhost/NStudent","root","sure";    
 con=DriverManager.getConnection(url);
                                     
                                     }
                                     catch(Exception e)
                                     {
                                            System.out.println("Connection Create Error");
                                      }
                                    return con;
                             }
                        }
           
Step_6:
                Create actionforms such as stuForms
                Include following code

package actionforms;

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

public class stuForms extends org.apache.struts.action.ActionForm
{
   
   private String stu_name;
   private int stu_rollno;

    public String getStu_name()
    {
        return stu_name;
    }
    public void setStu_name(String stu_name)
    {
        this.stu_name = stu_name;
    }
    public int getStu_rollno()
    {
        return stu_rollno;
    }
    public void setStu_rollno(int stu_rollno)
    {
        this.stu_rollno = stu_rollno;
    }

}

Step_7:

            Create action (I have used stuAction)

            Include following coding

package actions;

import bl.stubl;
import java.util.Vector;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

public class stuAction extends org.apache.struts.action.Action
{
   
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        stubl bl=new stubl();
        Vector v =bl.select();
               
        request.setAttribute("list", v);
        return mapping.findForward("select");
       
    }
}

Step_8:
            Create java class for ValueObject(vo)

            I have used as stuvo under Folder vo.
           
            Include following coding

package vo;

import java.io.Serializable;

public class stuvo implements Serializable
{
    private String stu_name;
    private int stu_rollno;

    public String getStu_name()
    {
        return stu_name;
    }
    public void setStu_name(String stu_name)
    {
        this.stu_name = stu_name;
    }
    public int getStu_rollno()
    {
        return stu_rollno;
    }
    public void setStu_rollno(int stu_rollno)
    {
        this.stu_rollno = stu_rollno;
    }
   
   
}
Step_9:
            Create Java class for Business Logic(bl)(I have used as stubl)

            Include following coding

package bl;
import Dbcon.dbcon;
import java.sql.*;
import java.util.Vector;
import vo.stuvo;
public class stubl
{
    Connection con=null;
    Statement stmt=null;
    ResultSet rs=null;
    Vector v=new Vector();
  
    public Vector select()
    {
        Vector v;
           
            v=new Vector();
                try
                {
                            Connection con=dbcon.getConnection();
                            Statement stmt=con.createStatement();
                            String sql="select * from NStudent";
                       
                            ResultSet rs=stmt.executeQuery(sql);
                       
                            while(rs.next())
                            {
                                    stuvo vo=new stuvo();
                                    vo.setStu_name(rs.getString(1));
                                    vo.setStu_rollno(rs.getInt(2));
                                    v.add(vo);
                            }
                }
                catch(Exception e)
                {
                       
                }
                return v;
               
            }
}
           



Step_10:
Create Home.jsp

Include

< %@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< html >
< head > < title > JSP Page < /title >
< body >
< html:form action="/stu" >
< html:submit value="OK"/ >
< /html:form >
< /body >
< /html >

Create list.jsp

< %@page import="java.util.*"% >
< %@ page import="vo.stuvo" % >
< %@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< html >
< head > < title > JSP Page < /title >
< body >

< %
Vector v=(Vector)request.getAttribute("list");

for(int i=0;i
{
stuvo vo=(stuvo)v.get(i);
out.println(vo.getStu_name());
out.println(vo.getStu_rollno());
}

% >
< /body >
< /html >

Step_11:


Configure struts-config.xml

….
….

< action-mappings >
< action
input="/"
name="stuForms"
path="/stu"
scope="session"
validate="false"
type="actions.stuAction" >
< forward name="select" path="/jsp/list.jsp"/ >
< /action >

< /action-mappings >

….
….

Step_12:

Run home.jsp

Conclusion:

From this post how to get data from database. Struts application with Database is very important when we are going to create web based project.


Do you have any doubt about this post you can post comments