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");

Similar Messages

  • 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.

  • 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!

  • 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.

  • Executing .sh files from a servlet

    Hi,
    Please clarify me on executing a .sh files from a servlet. or from a java class.
    Thanks.

    Runtime rt = Runtime.getRuntime();
    rt.exec(" <u r shell script file >");
    this works fine in java class... i dont exactly know weather it works in servlet or not.... check it out....
    jogesh

  • Error when executing a jsp from a screenflow

    I am getting the following exception when I try to run my jsp:
    Tomcat error: 'ApplicationDispatcher[workspace] Servlet.service() for servlet jsp threw exception
    org.apache.jasper.JasperException: Invalid BPM Object context key
    ----- Root Cause -----
    fuego.xobject.util.InvalidContextKey: Invalid BPM Object context key
    at fuego.xobject.util.XOContext.setJSPContext(XOContext.java:24)
    I traced the issue to a line in my jsp below:
    <f:invoke var="${loc_tpData}" methodName="getTxtDepartureCountryValues" retAttName="txtDepartureCountry" retAttScope="Session" />
    Could someone tell me what I am doing wrong please?
    Thanks.
    -muktech

    Has anything changed in the naming of the instance variable as I am encountering the same problem. My code used to work fine under 5.7 but it does not seem to be possible to access the variable using the syntax ${varnamefromscreenflow}.

  • 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

  • Execute a command from servlet

    Hello,
    How can I execute this command from a servlet ?
    example: I want execute "java -cp /root/:/root/log/api/:/root/log/apps/ Transmitter" when I lunch servlet1.java
    so which class in JAVA can do this ?
    Thanks ?

    why u want to run another program? Usually you call the methods within the second program.
    Is it possible to invoke another program in the server using a servlet? If so it could even mess up the whole server. I am not sure.

  • How to execute a JSP Page?

    Hi Experts,
    I have created a Abstract portal Component JspDynPage.
    the JSP is created in <Componnet Name>/dist/PORTAL_INF/pagelet/test.jsp
    if i want to execute a JSP from the browser directly what will be the URL to execute this jsp page.
    Regards,
    Viswes

    Thanks for your immediate response.
    Actually ive uploaded the par file and also created the iView with respect to that par.
    Actually My Requirement is in my project i have created another jsp file.I want to open a pop-up window for this jsp to open.
    for this to open in new window im using JavaScript. onClick event iam calling this window.open("test.jsp");
    but it is throughing me an error saying not found.
    So i need to specify the correct path to execute this jsp.
    Can you Please guide me?
    Thanks,
    Viswes

  • Executing an executable from a servlet or a JSP

    Is it possible to call an executable from a servlet. Say I have a client server application and I want to start the client application from a servlet. What is the best way to do that?
    I know normal Java applications can use the Runtime to run an executable. Can a servlet do the same? What are the security implications, considering that the servlet will be accessed from a browser?
    Thanks for your help.

    A servlet is executed on the server, not the client. The client receives a response from the servlet which is usually plain text formatted as HTML and/or javascript.
    So can a servlet use the Runtime class? Sure, but on the SERVER, not on the client. Hence, the executable will need to be on the webserver, not on the client machine and it will execute on the webserver, not on the client.
    Maybe, your HTML formatted response can include an <applet> tag to trigger the browser to download an applet that may have access to call an executable. Maybe a trusted applet?

  • Execute option missing from JSP drop down menu

    I'm going through a Java Web Development class and trying to produce a small web app through Sun Java Studio Enterprise 7 2004Q4. I have two servlets, a couple of classes and two JSPs. At seemingly random times the "Execute" and "Execute (force reload)" options from the drop down menu you get when you right click a JSP fails to show up. It isn't grayed out, it just isn't there. Most of the time these options are there. And they can be there one time and then for the same project, same set of JSPs everything, they just disappear.
    Can anyone tell me what dumb thing I'm doing to cause this?
    And can anyone tell me how to fix it once I've done it?
    Thanks
    DR Crowley

    First of all thank you for all the time you are spending on this. Believe me I know how frustrating it can be to figure out what is happening when you can't reproduce the problem.
    On to your questions: I am running Windows XP Professional. I have checked and made sure that the extension is still .jsp and that it isn't changing. No I don't have the IDE set up to show extensions, however the icon beside the file is the jsp icon and when I go to Windows explorer (where I do have it set to show known extensions) it is a .jsp file and does not have the ever exasperating hidden extension that windows is infamous for. Sun Studio seems to recognize the file as a proper jsp (due to the icon).
    As to your further questions. I am executing it by right clicking on the jsp file itself. And the jsp file is in the documents folder not in the web/inf folder.
    At this point we are out of the class and it isn't nearly as important that I get an fix. We (our company) will probably not do jsp files and servlets thourgh Sun Java Studio Enterprsise. Instead we are looking at using Sun Java Studio Creater to do the view layer.
    Thanks for all your help and the time you took to try to figure this out.

  • How to execute sql file from servlet

    Hi,
    I am using JSP, Servlets and Oracle 8i in my application. I want to execute .sql file from java code. Is it possible to do that,
    as we execute .sql file from sql plus prompt.
    Suppose I have abc.sql file and I want to execute it from java code.
    If any body have the solution then pl. reply with sample code.
    Thanks,
    Rajesh

    If any body have the solution then pl. reply with
    sample code.No, no, dec - s/he doesn't want the actual solution, but the full code!
    /k1

  • 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);
    }

  • How to display outsream from a servlet in a JSP

    I wrote a jsp file to call a Servlet, then want to display the result from the servlet in another jsp file. Anyone can give me possible solution or an example to illustrate it.
    Many thanks in advance.
    the file that call servlet in a jsp is as follow
    <form method="POST" action="myservlet">
    <table>
    <tr>
    <th align="center" colspan="2">
    Enter The query words
    </th>
    </tr>
    <tr>
    <th align="right">query words:</th>
    <td align="left">
    <input type="text" name="querywords" size="60">
    </td>
    </tr>
    <tr>
    <td align="right">
    <input type="submit" value="Search">
    </td>
    <td align="left">
    <input type="reset" value="Reset">
    </td>
    </tr>
    </table>
    </form>
    ............

    I would suggest that you use the Servlet to retrieve the data you are after and store it in the session. Then forward the user onto the JSP page where it can retrieve the object from the session and present it to the user in a formatted view.

Maybe you are looking for

  • How to find the table name on which integrity constraint not found

    Hi All How to acheive this I have a lot of tables with lot of primary key - foreign key relationship. In plsql when any inserts happen in the child table & the corresponding row is not present in the parent table, we get an exception ORA-02291: integ

  • Counting Line items in FBL3N

    Hi Gurus, If we execute a report FBL3N and system shown 110 items. We use a filter to filter out 15 items so how can we see in a system that how many items are remaining. Is there any facility to count these items? Please revert asap. Thank You

  • HCM Dynamic Applicant Creation

    Applicant Data Will me Maintained in EXCEL .CSV File. I tried to update the excel data thro BAPI To create a New Applicant IN PB30 Dynamically. But went in vain.Kindly Suggest me an Idea or is there any option to create an applicant dynamically other

  • Can't type the letter that comes a_ter the letter e....says active directory domain service is unavailable

    can't type the 6th letter o_ the alphabet......says active domain service is unavailable

  • JTabbedPane Issues

    JTabbedPane Issues: After finding little info on the subject (that worked for me anyway), I thought I'd post this in case anyone else searched for it. Removing JTabbedPane's Border the easy way: UIManager.put("TabbedPane.contentBorderInsets", new Ins