Solved

Redirecting from jsp to servlet,and back to original jsp

Posted on 2007-03-31
11
406 Views
Last Modified: 2013-11-24
I have a index.jsp page that i need to pass information from to a servlet, but i then need the servlet to redirect me back to the index.jsp page displaying the new data.Is this possible? And where should i place the code,within the doPost,doGet or the method that is processing the data from the bean.When i click on "Add" in jsp it is directing to servlet URL but it is a blank page displaying nothing

here is my list_add code

private void listAdd(String newTeam, HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException   //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
                   RequestDispatcher rd = getServletContext().getRequestDispatcher("MyWeb/web/index.jsp");
                   rd.forward(request, response);
         }

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

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

               }
                 
                   

         }

Regards,
Styx218
0
Comment
Question by:Styx218
11 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 250 total points
ID: 18829202
>> RequestDispatcher rd = getServletContext().getRequestDispatcher("MyWeb/web/index.jsp");

Perhaps the URL there is wrong. What is the URL pattern of the servlet?
0
 

Author Comment

by:Styx218
ID: 18829495
<servlet-name>AdminServlet</servlet-name>
 <url-pattern>/AdminServlet</url-pattern>
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18830270
the web app context is /MyWeb?

Maybe try: getRequestDispatcher ( "/web/index.jsp" ) ;
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Styx218
ID: 18832293
Hi, Thanks for your suggestions,but i have a new issue, i have altered my code but i am now getting following error when trying to load my jsp page

"Can't find a method to write property 'name' of type 'java.lang.String' in a bean of type 'com.football.Team'"

My bean code is
package com.football;

import java.beans.*;
import java.io.Serializable;

public class Team  implements Serializable
{
    private int    id;
    private String name;

    public Team()
    {
        id = id;
        name = name;
    }

    public int getId()
    {
        return id;
    }

    public String getName()
    {
        return name;
    }

    public boolean equals(Object obj)
    {
        if (obj instanceof Team)
        {
            Team compare = (Team)obj;
            return (id == compare.getId());
        }
        return false;
    }

    public int hashCode()
    {
        return name.hashCode();
    }

    public String toString()
    {
        return getClass() + "[" + id + "] : [" + name + "]";
    }



    private void setId(int id)
    {
        id = id;
    }

    private void setName(String name)
    {
        name = name;
    }
}

My bean declaration in index.jsp is as follows

<jsp:useBean id = "newTeam" class = "com.football.Team" scope ="request" />
<jsp:setProperty name = "newTeam" property = "name" value = "name" />

i feel i am getting closer to solving this, but i cant use the <jsp:getProperty in the AdminServlet as its a .java file. Any idea as to where i should go from here?

Regards,
Styx218
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18832340
Did you compile that class? Is its class file present in the WEB-INF/classes/com/football directory? Try restarting the server once after compiling it and then try - the class contains a getName () method so it should fine once the server loads it.
0
 

Author Comment

by:Styx218
ID: 18832447
Hi, Recompiled the class,restarted server and still same issue

my class file is contained within MyWeb\build\web\WEB-INF\classes\com\football as i am using NetBeans along with all other java class files..
0
 

Author Comment

by:Styx218
ID: 18832788
I have taken out the jsp:setProperty which seems to have removed the error,but i am now getting

C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\footy\MyWeb\src\java\com\football\AdminServlet.java:134: cannot find symbol
symbol  : constructor Team(int,java.lang.String)
location: class com.football.Team
                   manager.addNewTeamToList(new Team(0, newTeam)); //pass parameters to method

My Team constructor is as follows??
public Team()
    {
        id = id;
        name = name;
    }

?????
0
 

Author Comment

by:Styx218
ID: 18833184
Any ideas?
0
 

Author Comment

by:Styx218
ID: 18839795
Can anyone help me with this? Surely its not too hard of an issue?
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 18841561
>>symbol  : constructor Team(int,java.lang.String)
location: class com.football.Team
                   manager.addNewTeamToList(new Team(0, newTeam));


in there you are trying to do a new Team(0,newTeam); which means system is looking for a constructer with a signature like

public Team(int i, String name)

which you do not have in class "Team" and hence the error is correct.
0
 
LVL 3

Expert Comment

by:elissaqp
ID: 18868789
why redirecting to the same page? why on the servlet create the result you want? A servlet will show a blank page if you do not tell it what  to show.
As simple as adding this code on your servlet:
out.println("<html>");
out.println("<body>");
out.println("hello world!!!");
out.println("</body>");
out.println("</html>");
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumHeights  challenge 17 70
mapBully challenge 6 134
groovy example issue 10 97
tomcat startup error 5 65
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.

773 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