Solved

Add to database using jsp, beans and serlvets??

Posted on 2007-03-30
9
984 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
  • 3
  • 3
  • 3
9 Comments
 
LVL 11

Accepted Solution

by:
Manish earned 75 total points
Comment Utility
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
Comment Utility
>> String newName = request.getParameter("Name");
String newName = request.getParameter("name");//text box name...
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 50 total points
Comment Utility
What is the problem you are facing? Any errors exceptions or unexpected results
0
 

Author Comment

by:Styx218
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 50 total points
Comment Utility
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
Comment Utility
Do you have any servlet having name "name"?
0
 

Author Comment

by:Styx218
Comment Utility
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
Comment Utility
"is contained within my list_add  method within the database "

i meant within the servlet sorry
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> cannot find symbol"

For which one?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
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…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now