Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

Redirecting from jsp to servlet,and back to original jsp

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
Styx218
Asked:
Styx218
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
>> RequestDispatcher rd = getServletContext().getRequestDispatcher("MyWeb/web/index.jsp");

Perhaps the URL there is wrong. What is the URL pattern of the servlet?
0
 
Styx218Author Commented:
<servlet-name>AdminServlet</servlet-name>
 <url-pattern>/AdminServlet</url-pattern>
0
 
Mayank SAssociate Director - Product EngineeringCommented:
the web app context is /MyWeb?

Maybe try: getRequestDispatcher ( "/web/index.jsp" ) ;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Styx218Author Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
Styx218Author Commented:
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
 
Styx218Author Commented:
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
 
Styx218Author Commented:
Any ideas?
0
 
Styx218Author Commented:
Can anyone help me with this? Surely its not too hard of an issue?
0
 
KuldeepchaturvediCommented:
>>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
 
elissaqpCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now