Accessing String assigned in a JSP from a Servlet?

Hello all, I was wondering if there is a way to access a String object that was assigned by an HttpSession object in a JSP from a Servlet. What happens is, in my application a user logs in, an HttpSession object is instantiated and all of the user credentials are assigned within a JSP like:
HttpSession httpSession = request.getSession( false );
String userName = httpSession.getAttribute("userName);
...{code}
Next, I have a Servlet (which is really just a Proxy to a different server), that I need to log some information with, namely the userName String. Is there a way I can access that String without instantiating another HttpSession object? Because when the timeout occurs (or when the user clears his cache I have found), the HttpSession becomes invalidated (even if I use  +HttpSession#getSession( true )+) and the +HttpSession#getAttribute+ call fails (userName is just null). So is this possible to do?
Thanks in advance!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

dnbphysicist wrote:
I understand you cannot recover attributes from a dead session, this is why I wanted to be able to access a variable that is pulled from the Session immediately after login so I can still get to it even once the session is dead.
Normally a session doesn't go dead after login.
I imagine that by using application scope the app would confuse the userName with other users that are logged in?Certainly. It was just an example. But your data clearly belongs in the session, so just keep it in the session. If the session get invalidated while it should not get invalidated, then your problem truly lies somewhere else.
I am definitely not arguing you latter point either :) A lot of this code I inherited unfortunately from previous developers and we are in desperate need of a redesign.I wish you much luck with that.

Similar Messages

  • Generating the JSP from the Servlet source file

    Hi,
    Does anyone out there know if there is any utility/class/program which will generate a JSP from the Servlet Source file (.java file) or indeed from the compiled Servlet file (.class file)
    If its possible to decompile a .class file into a .java file, surely it must be possible to convert the .java souce file into the .jsp file that created it.
    any comments would be appreciated
    Richard

    I am the author of the original JSP!
    But I want to run a script on the generated Servlet source file,
    changing its contents, and then generate the JSP that would have created the new Servlet source file.
    but I need to know if theres anything that will reverse engineer a Servlet source into the generating JSP.
    its a kind of of Jeopardy type thing!

  • Executing a jsp from a servlet

    Hello all
    I am a starter to JSP.
    In an application I am developing, I have a servlet which receives a request from a form. If the user asks for a tabular representation of data (from the database), I want to use a JSP to display such a table since that way, I do not have to write all those out.printlns used in a servlet.
    So is there a way to either pass control to a jsp from a servlet? I want to pass a hashtable and some data to the jsp from the servlet.
    Please help with possible suggestions and code.
    thanks a lot....
    Mahesh :)

    There is the RequestDispatcher object to forward request to another resource (as servlet or jsp).
    If in your servlet you code:
    RequestDispatcher rd=getServletContext().getRequestDispatcher("your JSP URL");//instantiate a RequestDispatcher object
    //sets Hastable as request attribute
    request.setAttribute("yourAttributeName",yourHashtableName);
    rd.forward(request,response);//forwards control to jsp
    In your JSP you will retrieve Hashtable by following code:
    Hastable ht=(Hashtable)request.getAttribute("yourAttributeName");

  • Passing parameter to a jsp from a servlet

    Hi,
    I need to pass a parameter from a servlet to a jsp without using the session object. Is it possible? I've been looking for a method like response.setParameter but it doesn't exist.
    Please help !!!

    Actually, there is no setParameter method in the ServletRequetsObject. Still, you can create one. This can be usefull when you have a jsp that get its parameters from either a direct html form posting or a forward from a servlet. This can be achieved using wrappers, instead of the real request object.
    It's not really tricky, it's only a little "underknown" feature.
    Here is a sample code when you can fake the settings of requets parameters :
    public class MyRequest extends HttpServletRequestWrapper {
       private HashMap fakedParameters;
       public MyRequest(HttpServletRequest nested) {
          super(this);
          fakedParameters=new HashMap();
        public void setParameter(String key,String value) {
            fakedParameters.put(key,value);
        public String getParameter(String name) {
           String res=fakedParameter.get(name):
           if (res==null)
               res=super.getParameter(name);
           return res;
    }NB: this is a sample, you may also override other method (such as getParameterNames) for a full featuerd wrapped request.
    Now, you can use your wrapped request :
    MyRequest req2=new MyRequest(request);
    RequestDispatcher dispatcher=request.getRequestDispatcher("/somePage.jsp");
    req2.setParameter("name","yourName");
    dispatcher.formard(req2,response);Hope it helps.

  • Access protected web folder with jsp code or servlet transparently

    Hi.
    I'm developing a web site. I want to place some of the web pages (*.swf)
    a protected web folder so that no one can DIRECTLY access them through wget or browser.
    However these *.swf files will be called by other jsp or servlets to display their content
    in broswer. What kind of authentication mechanism should I use? Should I use filter?
    Please give me some ideas.
    Best regards.

    A Filter is perfectly applicable. But you also have to define the requirements which a request must fullfill to retrieve the contents. For example a login mechanism of which the User is been stored in the session, or an unique key provided as request parameter, or maybe the referrer, etcetera.

  • Invoking JSP from custom Servlets

    I am building a Servlet on Tomcat. The path to access this is "/"; so basically any HTTP request made to Tomcat will pass it through to my Servlet.
    There are a few cases were I would like the default JSP Servlet to parse a JSP page, however I am unable to instantiate it. Is there a way to do this?
    Thanks,
    Jeremy

    You could always make a URL request. That's probably the only way

  • Forward to a specific area of a jsp from a servlet

    I know I could use request.sendRedirect("/myjsp.jsp#content") but this method doesn't transfer the request object. I need the attributes stored in the request object.
    Is this possible without using a redirect?
    Thanks for your help.

    refer RequestDispatcher()
    u get solution

  • Assigning a node value from an XML variable to a String type  in Weblogic Process Integrator

    Hi,
    Is there any way to assign a node value from an XML variable to a String variable
    in Weblogic Process Integrator...
    Thanx.
    Narendra.

    Nerendra
    Are you talking about using Xpath on the XML document and assigning to a
    variable, it is unclear what you are asking
    Tony
    "Narendra" <[email protected]> wrote in message
    news:3bba1215$[email protected]..
    >
    Hi,
    Is there any way to assign a node value from an XML variable to a Stringvariable
    in Weblogic Process Integrator...
    Thanx.
    Narendra.

  • How do I Display a string from a servlet into a JSP Page???? NEED HELP!!!!

    Hi guys,
    How do I Display a string from a servlet into a JSP Page...
    Ive tried so many bloody things!.....
    Simply.
    I get text from JSP. The servlet does what ever it does to the string.
    Now. Ive create sessions and bean things,.... how the hell do I display it in a text box... I can display on the screen.. but not in the text box.!!!
    please help!!!

    hmmm, I dont really like using JSP programming, u should be using JAVA..
    the way to do it is:
    Call and cast to the bean like this:
    <%@ page import="beans.*" %>
    <% //cast to bean get request create object
    userNameBean u= (userNameBean) request.getSession().getAttribute("userNameBean");
    then... all you do is call it like this:
    <input type="text" name="firstName" value="<%= u != null? u.getFirstName(): "" %>">
    this is the real programmers way,,,
    chet.~

  • Accessing a jsp from excel

    Hi,
    I have a jsp page in my weblogic server(version 9.2). It simply sets a session variable when the user visits the page for the first time. Now i am trying to access this jsp from a excel file(I am providing the URL of the page as a hyperlink). But the page is getting processed twice.
    For example in my jsp I have the following code:
    <%
    System.out.println("Initially "+session.getAttribute("var"));
    session.setAttribute("var","JAVA");
    System.out.println("After setting"+session.getAttribute("var"));
    %>
    When I access this page from excel it is printing the following in console:
    Initially null
    After settingJAVA
    Initially null
    After settingJAVA
    As you can see the statements are getting printed twice.
    Can any one explain the reason for such a behaviour?
    Edited by: gopi_3nath on Apr 3, 2008 4:35 AM

    Just the same as normal HTML
    In child window
    window.opener.parentfunctionname()

  • How to call a jsp file from an servlet and access the jsp file objects??

    Hi everybody
    I have an jsp file where it contains a vector object with some data and I have a servlet that needs to access this vector object . How can my servlet call for this jsp page and get the vector object. ?
    then an applet will be calling this servlet to get some other object created with the data contained within this vector object...so each time Applet ask the servlet for the object the servlet need to call for this jsp page somehow
    My main question is the communication servlet-jsp
    please if u have any clue I will appreciate it
    thanks

    Hi
    There are several ways to share objects between JSPs/Servlets.
    You can share objects by putting them in the request-in which case the objects lifetime is limited by that of the request, Another way is to store them in the session, againg the lifetime is limited by the lifetime of the session. To have objects that have to persistent over the application life-span use the servletContext to store the objects.
    Any good tutorial should help you get started. Please see the link below for a tutorial on Servlets/JSPs
    Note: Local variables in the JSP cannot be shared with other components as their scope is limited to that particular Page/Servlet.
    Link: http://java.sun.com/docs/books/tutorial/servlets/
    Good Luck!
    Eshwar Rao
    Developer Technical Support
    Sun microsystems inc
    http://www.sun.com/developers/support

  • How can I assign image file name from Main() class

    I am trying to create library class which will be accessed and used by different applications (with different image files to be assigned). So, what image file to call should be determined by and in the Main class.
    Here is the Main class
    import org.me.lib.MyJNIWindowClass;
    public class Main {
    public Main() {
    public static void main(String[] args) {
    MyJNIWindowClass mw = new MyJNIWindowClass();
    mw.s = "clock.gif";
    And here is the library class
    package org.me.lib;
    public class MyJNIWindowClass {
    public String s;
    ImageIcon image = new ImageIcon("C:/Documents and Settings/Administrator/Desktop/" + s);
    public MyJNIWindowClass() {
    JLabel jl = new JLabel(image);
    JFrame jf = new JFrame();
    jf.add(jl);
    jf.setVisible(true);
    jf.pack();
    I do understand that when I am making reference from main() method to MyJNIWindowClass() s first initialized to null and that is why clock could not be seen but how can I assign image file name from Main() class for library class without creating reference to Main() from MyJNIWindowClass()? As I said, I want this library class being accessed from different applications (means different Main() classes).
    Thank you.

    Your problem is one of timing. Consider this simple example.
    public class Example {
        public String s;
        private String message = "Hello, " + s;
        public String toString() {
            return message;
        public static void main(String[] args) {
            Example ex = new Example();
            ex.s = "world";
            System.out.println(ex.toString());
    }When this code is executed, the following happens in order:
    1. new Example() is executed, causing an object to constructed. In particular:
    2. field s is given value null (since no value is explicitly assigned.
    3. field message is given value "Hello, null"
    4. Back in method main, field s is now given value "world", but that
    doesn't change message.
    5. Finally, "Hello, null" is output.
    The following fixes the above example:
    public class Example {
        private String message;
        public Example(String name) {
            message = "Hello, " + name;
        public String toString() {
            return message;
        public static void main(String[] args) {
            Example ex = new Example("world");
            System.out.println(ex.toString());
    }

  • Accessing bean properties in a jsp

    Hi,
    I am trying to access a bean in my jsp page like this,
    <c:out value="${clientSession.customerName}"/>
    I validate and then set the bean in my action class, I am using struts 1.1,
    package com.neo.five.ereports;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionForm;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpSession;
    import com.neo.five.cookies.SessionCookieValidator;
    import com.neo.five.beans.CustomerSessionBean;
    public final class EreportAction extends Action{
         public ActionForward execute(ActionMapping mapping,
                                            ActionForm form,
                                            HttpServletRequest request,
                                            HttpServletResponse response){
              //get the session object
              HttpSession session = request.getSession(false);
              //validate the session cookie if session object dows not exist
              Cookie[] cookies = request.getCookies();
              SessionCookieValidator scv = new SessionCookieValidator(cookies);
              scv.checkCookie();
              //forward to appropriate destination
              //if there is a cookie continue
              if(scv.hasSessionCookie()){
                   //check for session object
                   if (session != null) {
                        //if session does not exist put bean into session that holds the cookie
                        //values
                        if(session.getAttribute("cookieSession")==null){
                             CustomerSessionBean sb = new CustomerSessionBean(scv.getCustomerid(),scv.getCustomerName(),scv.getCfid(),scv.getCftoken());
                             session.setAttribute("cookieSession", sb);
                             request.setAttribute("clientSession",sb);
                        return (mapping.findForward("success"));
                   }else{
                        return (mapping.findForward("failure"));
              }else{
                   return (mapping.findForward("failure"));
    }this is my SessionBean
    package com.neo.five.beans;
    public class CustomerSessionBean {
         private int customerid;
         private String customerName;
         private int cfid;
         private int cftoken;
         public CustomerSessionBean(int customerid,String customerName,int cfid,int cftoken){
              this.customerid=customerid;
              this.customerName=customerName;
              this.cfid=cfid;
              this.cftoken=cftoken;
         public int getCustomerid(){
              return customerid;
         private String getCustomerName(){
              return customerName;
         private int getCfid(){
              return cfid;
         private int getCftoken(){
              return cftoken;
    }however I am getting this error,
    HTTP Status 500 -
    type Exception report
    message
    description The server encountered an internal error () that prevented it from fulfilling this request.
    exception
    org.apache.jasper.JasperException: An error occurred while evaluating custom action attribute "value" with value "${clientSession.customerName}": Unable to find a value for "customerName" in object of class "com.neo.five.beans.CustomerSessionBean" using operator "." (null)
         at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
         at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
         at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
         at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
         at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:432)
         at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:356)
         at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
         at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
         at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
         at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
         at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
         at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
         at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
         at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
         at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
         at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
         at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
         at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
         at java.lang.Thread.run(Thread.java:536)
    root cause
    javax.servlet.ServletException: An error occurred while evaluating custom action attribute "value" with value "${clientSession.customerName}": Unable to find a value for "customerName" in object of class "com.neo.five.beans.CustomerSessionBean" using operator "." (null)
         at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:533)
         at org.apache.jsp.eReportDeskTopRoot_jsp._jspService(eReportDeskTopRoot_jsp.java:145)
         at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
         at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
         at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
         at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
         at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
         at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:432)
         at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:356)
         at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
         at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
         at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
         at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
         at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
         at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
         at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
         at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
         at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
         at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
         at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
         at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
         at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
         at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
         at java.lang.Thread.run(Thread.java:536)
    Apache Tomcat/4.1.30
    thanks for any help.

    how?.... mmmm ... did ... (head scratch) ...I miss that.
    Thanks a million for spotting that, I really did appreciate that.
    jp.

  • Challenge forwarding from a servlet to a JSP

    Redirecting information from a servlet to a JSP
    Hello Everyone,
         I am processing the contents of a html form using a servlet. After processing the
    information received and storing it into a javabean I am trying to pass control onto a jsp for
    display. At this stage the jsp does not display as desired due to a URL challenge. The URL
    which references my jsp is:
    http://localhot/ferngully/prefset.jspThis url has been designated to the jsp page 'prefset.jsp' by the application specific web.xml
    file (...I am using Tomcat 5!), the relevant contents of which follow:
    <servlet>
         <servlet-name>PreferenceSet</servlet-name>
         <jsp-file>/prefset.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
         <servlet-name>PreferenceSet</servlet-name>
         <url-pattern>/ferngully/prefset.jsp</url-pattern>
    </servlet-mapping>If I access the page with the afore mentioned URL it displays correctly. However the URL which
    is returned to the browser after the servlet has forwarded control to the JSP is:
    http://localhost/ferngully/ferngully/PreferencesThe following form markup is used to access the servlet:
    <form name="form1" method="post" action="ferngully/Preferences"> I know that I have mapped the servlet correctly because the servlet performs it's programatic tasks happily :)
    I am using code within the servlet to forward control to the JSP, and I believe that this is the crux of my challenge.
    The code for the servlet is listed below.
    package userpreferences;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    public class PreferencesServlet extends HttpServlet
         public void init(ServletConfig config) throws ServletException
              super.init(config);
         public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
              doPost(request, response);
         public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
              String url = "/prefset.jsp";
              ServletContext servCont = getServletContext();
              RequestDispatcher reqDispatch = servCont.getRequestDispatcher(url);
              reqDispatch.forward(request, response);
    }I believe that the above highlighted code section is responsible for my challenge. I have tried changing the String url to "/ferngully/prefset.jsp",
    however there was no improvement in functionality.
    Can somebody please explain to me how I can go about directing the control of this process from the servlet to the jsp correctly? Particularly
    how I can go about making the url returned to the client correct for accessing the jsp page? Do I need to delete the mapping entry for the
    jsp page in web.xml? I am new to servlets & jsp so any help will be much appreciated.
    Thanks
    Kind Regards
    Davo

    3. >The following form markup is used to access the servlet:
    <form name="form1" method="post" action="ferngully/Preferences">Question 1:
         why do you specify your contextname in action attribute again ?
         General funda about urls:
         When you have an index.html loaded through http://localhost/index.html, then a link in that html whose url is,
         1. say, preferences.html (no forward slashes preffixed or suffixed) would load the html from the same folder.
         2. say, /ferngully/prefences.html, would attempt to load a html from the ferngully folder that is a sub-directory
    of your web-application
         3. say, /ferngully/prefences.jsp, would attempt to load a html from the ferngully folder that is a sub-directory
    of your web-application
         When you have a servlet whose url-pattern is set to intercept requests, it would intercept requests that have that
    pattern it is defined to intercept in web.xml (for example all the above requests in 1,2 and 3 would be intercepted
    by a servlet whose pattern is ferngully/*)
         Now if your action is not a specific resource (.jsp or .html), something like /preferences, this would map to a url
    /ferngully/preferences. (for which a resource should be defined in web.xml - either a servlet or a jsp)
    However the URL which is returned to the browser after the servlet has forwarded control to the JSP is:
    http://localhost/ferngully/ferngully/PreferencesTell us the url of the original html
    I know that I have mapped the servlet correctly because the servlet performs it's programatic tasks happily :)Tell us the url-appetrn of the servlet that intercepted your request
    Particularly
    how I can go about making the url returned to the client correct for accessing the jsp page?The url in the browser is always the request url, you cannot change it to the url of the jsp even which rendered the page
    through a fwd from the servlet.
    Do I need to delete the mapping entry for the
    jsp page in web.xml?Not necessary. You can access the jsp directly from the servlet while forwarding by
    1. specifying the exact path to the jsp. For example if the jsp is under a folder called jsps, your request dispatcher
    url ought to be /jsps/prefset.jsp
    2. specifying the jsp's url-pattern as specified in web.xml
    Questions Contd
    2. Why does the url pattern have to have a .jsp suffixed.
    From your code
    <url-pattern>/ferngully/prefset.jsp</url-pattern>It could equally well have been
    <url-pattern>/ferngully/prefset</url-pattern>And then your servlet would have to forward it to ("/ferngully/prefset")
    2. Your jsp-file mapping is in this form
    <jsp-file>/prefset.jsp</jsp-file>This assumes that the jsp is in the top-level folder of your web-application and not under
    any sub-directories. Correct ?
    cheers,
    ram.

  • Getting to next JSP from Servlet

    I am getting a 404 when I try to redirect from my servlet to an error page. The code that I am testing is....
    catch (SQLException es)
    LOG.error("Unexpected error in Login.createUser.Error
    message = " + es);
    session.setAttribute(Constants.MESSAGE, es.getMessage());
    session.setAttribute(Constants.ERROR_TITLE, "Login error: " + Constants.SQL_ERROR);
    resp.sendRedirect(Constants.ERROR_PATH);
    I have a compiled java class called Constants where the ERROR_PATH is defined as
    /** Constant name used in obtaining the path to the error page. */
    public static final String ERROR_PATH = "/Jsp/error.jsp";
    My JSP pages are located at the root...
    myapp
    ....Jsp
    ....WEB-INF
    ........classes
    My web.xml looks like this
    <servlet>
    <servlet-name>errorPage</servlet-name>
    <jsp-file>/Jsp/errorPage.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
    <servlet-name>error</servlet-name>
    <url-pattern>/Jsp/error.jsp</url-pattern>
    </servlet-mapping>
    Can anyone point me in the right direction....Thanks

    I tend to use a utility method called redirectToResource in particular to redirect from any servlet to any JSP :
    /** This method will still work when the server is configured to
    *   listen to  port 80 rather than port 8080 (default in Tomcat)
    public void redirectToResource (HttpServletRequest req,
                                     HttpServletResponse resp,
                                  String resourceName)
                         throws ServletException, IOException
       int serverPort    = req.getServerPort();
       String scheme     = req.getScheme();
       String serverName = req.getServerName();
       StringBuffer urlBuffer = new StringBuffer(40);
       urlBuffer.append(scheme + "://" + serverName);
       urlBuffer.append(":" + serverPort);
       urlBuffer.append(resourceName);
        String location = resp.encodeRedirectURL(urlBuffer.toString());
        resp.sendRedirect(location);
    }

Maybe you are looking for

  • Query takes 5 min when using Indexes and 1 Second without Indexes !!

    Hi, We have been using indexes on all tables until recently when we faced problems with queries like the one below: SELECT a.std_id FROM students a, student_degree b, student_course c, course d WHERE b.std_id = a.std_id AND c.std_id = a.std_id AND d.

  • Applet: update file on server

    Hi Forum, Following is the code in an applet to update file on server. Why does the following code is not doing any updation to the server file? Any reply will be appreciated. protected void SaveFile() { try { URL url = new URL("Http://uptsrv/draw/Sa

  • File is shown as not saved even if it's just opened

    I recently upgraded Logic Express 8 to 9, the downloaded and installed updates. When I open any Logic project it shows up as unsaved (window closing widget has black dot in it). I save project as Logic 9 file, close it, but when I reopen it, again bl

  • A page element is not copied

    Halo, Firefox Users. Firefox 22.0 on Windows7. I have seen this issue for such a long time. Is this a bug ? Please, visit this site by using Firefox. http://answers.microsoft.com/en-us/office/forum/office_2010-onenote/onenote-linked-notes-window-is-d

  • ANOTHER CLIENT FOR  4.7 IDES SYSTEM

    1) I have 800 client,but i want to open another client ,Please explain procedure? 2) In new client,I must able to do all operations what iam doing with my present client 800? Kindly help me.