Solved

Add to database using jsp, beans and serlvets??

Posted on 2007-03-30
9
994 Views
Last Modified: 2013-11-24
I am trying to pass a form from my index.jsp page to a servlet, where the data will be added to a database. I am using mvc so none of the processing is done in the servlet, but i cannot get the form to pass to the servlet. i am using netbeans 4.1 with a sql database.I have been trying to solve this for some time so any feedback would be much appreciated.

index.jsp
declaring the bean
package com.football
<jsp:useBean id="team" class="com.football.NewBean" scope="session" />
<jsp:setProperty name = "team" property = "name" value = "name" />
<% session.setAttribute ("team", team); %>

then the form
<FORM action = "team" method="POST">
      
      <input type="hidden" name="ACTION" value="LIST_ADD" />
      <tr>
        <td colspan="2"><b>Add new Team</b></td>
        <td><input name="name" type="text" size="20" value= ="team.name"  /></td>
        <td><input name="ADD" type="submit" value="Add" /></td>
        <td>&nbsp;</td>
      </tr>
        
  </table>


NewBean
public class NewBean  implements Serializable {
   
    public String name;
    public int id;

    public NewBean() {
        name = null;
    }

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

          public String getName() {
        return name;
    }
         
          public void setId(int id)
          {
           this.id = id;  
         
          }
         
         
          public int getId()
          {
          return id;
          }
}


AdminServlet(containing LIST_Add method)
public void doGet(HttpServletRequest request, HttpServletResponse response) //Process incoming get request
            throws ServletException, IOException
    {
        doPost(request, response);
         HttpSession session = request.getSession();
        NewBean myBean = (NewBean)session.getAttribute("team");

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) //Process incoming post request
            throws ServletException, IOException
    {
        String action = findAction(request); //identify which action has been requested

       if (ACTION_LIST_ADD.equals(action))
   {
       String newName = request.getParameter("Name");
       if (isValidString(newName))
       {

            try
            {
                listAdd(newName);
            }
            catch (SQLException e)
          {
               e.printStackTrace();
            }

         }

   }


private void listAdd(String newTeam) throws SQLException   //process Add request from browser
   {
         FootyManager manager = null; //create instance of class
           
         
           

         try

         {
               manager = getFootyManager();  //call method to get connection
               manager.addNewTeamToList(new Team(1, newTeam)); //pass parameters to method
         }

         catch (SQLException e)
                           {
                                 e.printStackTrace();
                      }

         finally
         {
               if (manager != null)
               {
                     manager.releaseResources();
               }

         }

   }

Any ideas guys?



0
Comment
Question by:Styx218
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 11

Accepted Solution

by:
Manish earned 75 total points
ID: 18823707
First I think u should move your logic written in get method to post method.
And call doPost method (As you are already calling..)
0
 
LVL 11

Assisted Solution

by:Manish
Manish earned 75 total points
ID: 18823714
>> String newName = request.getParameter("Name");
String newName = request.getParameter("name");//text box name...
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 50 total points
ID: 18825566
What is the problem you are facing? Any errors exceptions or unexpected results
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Styx218
ID: 18826156
What i am trying to do is to add to database when  i click on "Add" but i am getting http error 404

" The requested resource (/MyWeb/team) is not available."
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 50 total points
ID: 18827660
have you defined a URL pattern team for some servlet in your web.xml?
0
 
LVL 11

Assisted Solution

by:Manish
Manish earned 75 total points
ID: 18828316
Do you have any servlet having name "name"?
0
 

Author Comment

by:Styx218
ID: 18828518
Hi, Thanks for your suggesstions guys,

I have a servlet pattern as below in web.xml

 <servlet-name>AdminServlet</servlet-name>
 <url-pattern>/footballexample</url-pattern>

My form action is as such in index.jsp

<FORM action = "com.football.AdminServlet" method="POST">
      
      <input type="hidden" name="ACTION" value="LIST_ADD" />
      <tr>
        <td colspan="2"><b>Add new Team</b></td>
        <td><input name="name" type="text" size="20"  /></td>
        <td><input name="ADD" type="submit" value="Add" /></td>
        <td>&nbsp;</td>
      </tr>

Will this work? I am trying to pass to servlet, which passes request to another class to add to database and then redisplay the index.jsp with the updated information in the dataase

// RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
   // rd.forward(request, response);

is contained within my list_add  method within the database but request is returning error "cannot find symbol"

I am aware this topic has been going on for a while but i hve no where else to turn to with this.Thanks again for your help
0
 

Author Comment

by:Styx218
ID: 18828519
"is contained within my list_add  method within the database "

i meant within the servlet sorry
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18829016
>> cannot find symbol"

For which one?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question