Solved

Redirecting from jsp to servlet,and back to original jsp

Posted on 2007-03-31
11
402 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:
mayankeagle 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:mayankeagle
ID: 18830270
the web app context is /MyWeb?

Maybe try: getRequestDispatcher ( "/web/index.jsp" ) ;
0
 

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:mayankeagle
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countClumps  challenge 10 90
Enhanced For loop vs ForEach Loop 4 58
allStar challenge 1 43
What is the latest versions eclipse neon 2 123
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 …
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.

744 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