Solved

Redirecting from jsp to servlet,and back to original jsp

Posted on 2007-03-31
11
408 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

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

Suggested Solutions

Title # Comments Views Activity
mapShare challenge 13 148
HashMap Vs TreeMap 12 68
jdbc error in jsp application 20 74
ejb example issues 3 56
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.

697 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