Solved

Add to database using jsp, beans and serlvets??

Posted on 2007-03-30
9
1,000 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

705 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