Results 1 to 13 of 13
  1. #1
    shiva is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default Reading parameter from JSP page

    HI

    I want to read the filename from the jsp by pressing the particular button for a particular file...and pass it on to servlet. This is not working. The code given below

    JSP page:

    <form METHOD="post" ACTION="DisplayLog">
    <div id="filelist">
    <%
    Object obj = request.getAttribute("filelist");
    if(obj != null) {
    String[] fileList = (String[]) obj;
    for (int i = 0; i < fileList.length; i++) {
    %>


    <input type="button" id="file" name="file" value=" <%=fileList[i]%>" onclick="display('<%=fileList[i]%>')"/>

    <br />

    <%
    }
    }
    %>
    </div>
    </form>


    My servlet file im reading as

    String filename = request.getParameter("file");



    im could not able to read the file name in servlet ...please let me know


    Thanks & Regards
    shiva

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    802
    Rep Power
    6

    Default

    U are having various ways to pass parameter from jsp to servlet.
    1.Either u can submit the form in jsp and u can get the values
    using request.getParameter("something")
    or
    u can send via session
    or
    u can pass as a query string
    etc etc....so many ways.
    U send me the code ,I can help u.What u told is not that clear.
    Ramya:cool:

  3. #3
    shiva is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    This is my filelist.jsp, in reading the files list from a readfile servlet and display it on jsp page. When i click the buttion of particular file name it should send to servlet DisplayLog which read the faile name and display its contents. My code need to change in display(fileName) fucntion... how could i pass the fileName to the servlet.


    <html>
    <head>

    <script type="text/javascript">
    function display(fileName) {
    document.location.href = "DisplayLog";
    // response.sendRedirect("/DisplayLog?param1=fileName");
    return false;
    }

    </script>

    </head>
    <body>

    <center>
    Please enter the application path and click submit...!
    </center>
    <div align="right">
    [
    <A href="disp.jsp" style=""><font
    style="font-family: verdana; font-size: 70%;" color="blue"><b>M</b>ain Page</font> </A> ]
    </div>
    <HR WIDTH="100%" SIZE="3" NOSHADE>
    <form name="form1" METHOD="post" ACTION="ReadFile">
    Enter the Path:
    <BR>
    <input type="text" name="Path" id="Path" size="10" />
    <input type="submit" value="Go" onclick="pathCheck()"/>
    </form>
    <BR>
    Debug Mode:
    <BR>
    <div id="filelist">
    <%
    Object obj = request.getAttribute("filelist");
    if(obj != null) {
    String[] fileList = (String[]) obj;
    for (int i = 0; i < fileList.length; i++) {
    %>


    <input type="button" id="file" name="file" value=" <%=fileList[i]%>" onclick="display('<%=fileList[i]%>')"/>

    <br />

    <%
    }
    }
    %>
    </div>


    <!--
    <form METHOD="post" ACTION="DisplayLog">
    <input type="text" name="file" id="fileid" size="18" />
    <input type="submit" name="filesubmit" value="fileval"/>
    <input type="Reset" name="reset" value="Reset" />
    </form>
    -->

    <form METHOD="post" ACTION="LogoutServlet">
    <div align="right">
    [
    <A href="logout.jsp" style=""><font
    style="font-family: verdana; font-size: 70%;" color="blue"><b>L</b>ogout</font>
    </A> ]
    </div>
    </form>
    </body>
    </html>


    DisplayLog.java

    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.Reader;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.log4j.Logger;
    public class DisplayLog extends HttpServlet {
    /**
    *
    */
    private static final long serialVersionUID = 1L;

    public void service(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
    String path = request.getParameter("file");
    //String path1 = request.getParameter("");

    LogMessages msgs = new LogMessages();
    //NonCatalogLogger myLogger = null;
    PrintWriter out = response.getWriter();

    msgs.setMessages(path);
    request.setAttribute("msgs", msgs);
    request.getRequestDispatcher("/display.jsp").forward(request, response);

    }
    }


    Thanks and Regards
    shiva

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    802
    Rep Power
    6

    Default

    whether "ReadFile" servlet is setting the FileList attribute like this below ?

    request.setAttribute("filelist",something).


    U send the complete code so that we can trace where it is wrong.Please send as an attachment.

    -Regards
    Ramya
    Ramya:cool:

  5. #5
    shiva is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    Hi,

    Please find the enclosed code ...

    Thanks and Regards
    shiva
    Attached Files Attached Files

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    802
    Rep Power
    6

    Default

    Hi Shiva,
    I got the issue.In filelist.jsp u are not sending any parameter know?
    But u are trying to retreive th "file" param in DisplayLog.java".

    function display(fileName) {

    document.location.href = "DisplayLog";

    return false;
    }
    Ramya:cool:

  7. #7
    shiva is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    That what i want to know. How to pass the parameter to the servlet. I tried using the function but it wont works:

    function display(fileName) {

    response.sendRedirect("/DisplayLog?file=fileName");
    return false;
    }

    Thanks & Regards
    shiva

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    802
    Rep Power
    6

    Default

    Hi Shiva,
    One small suggestion.
    1.do this Change this html portion.
    <input type="button" id="file" name="<%=fileList[i]%>" value="<%=fileList[i]%>" onclick="display('<%=fileList[i]%>')"/>

    2.do this in function
    function display(filelist) {
    document.location.href = "DisplayLog.jsp?file="+document.getElementById(fil elist).value;

    return false;
    }
    Ramya:cool:

  9. #9
    shiva is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    Hi,

    I have check the above code ...its not working , its not calling the DisplayLog.java servlet.

    it calls the servlet wehn i just use document.location.href = "DisplayLog"; but i could not able pass the parameter file. Please let me know

    Thanks & Regards
    shiva

  10. #10
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    802
    Rep Power
    6

    Default

    Instead of servlet I have used jsp because of some problem with my system.u mention servlet path here.
    Any error it is throwing ?
    1.do this Change in html portion.
    <input type="button" id="file" name="<%=fileList[i]%>" value="<%=fileList[i]%>" onclick="display('<%=fileList[i]%>')"/>


    2.do this in function
    function display(filelist) {
    alert(document.getElementById(filelist).value);
    document.location.href = "give servlet here?file="+document.getElementById(filelist).valu e;

    return false;
    }
    Last edited by RamyaSivakanth; 05-06-2009 at 06:21 AM.
    Ramya:cool:

  11. #11
    shiva is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    It not working ...not errors, it wasnt invoke the servlet

  12. #12
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    802
    Rep Power
    6

    Default

    One questions for u.
    Like "ReadFile" servlet have u mentioned the "DisplayLog" servlet also in servletmapping?

    Instead of a servlet I have taken ur DisplayLog servlet code and made it as jsp and it worked.Just check ur servlet mapping.
    Ramya:cool:

  13. #13
    Sapan is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default Help

    I have similar doubt as Shiva!
    but I couldn't figure out the solution.

    I would like to tell my problem-

    When trying to pass message from Java Servlet to Jsp file using
    request.setAttribute("message", "This is my message text");

    but instead of this I want to send a file name. I don't want to send simple string "This is my message".

    Any help will be deeply appreciated.

Similar Threads

  1. passing a parameter
    By aarthi2learn in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2008, 05:46 AM
  2. Can a method take itself as parameter?
    By bukake in forum New To Java
    Replies: 10
    Last Post: 09-06-2008, 09:26 PM
  3. Retrieving a parameter through URL.
    By hisouka in forum Java Servlet
    Replies: 1
    Last Post: 09-06-2008, 12:45 PM
  4. Replies: 1
    Last Post: 08-22-2008, 06:28 PM
  5. arugment/parameter
    By ravian in forum New To Java
    Replies: 5
    Last Post: 01-04-2008, 09:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •