3

This is my Servlet:

    protected void doPost(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");

    String collectionName = request.getParameter("myCollectionName");
    response.sendRedirect("index3.html");
    String pattern = request.getParameter("Pattern");
    String notPattern = request.getParameter("NotPattern");
    }
}

This is my how my first html page looks like: https://i.stack.imgur.com/oFlQD.png

After the user clicks create, my web app redirects the user to the next page which looks like this:https://i.stack.imgur.com/UkfGd.png

I would like to use the value of Collection Name from my first html web page. In my second html page, I want the "Edit Collection" text box to have the same value as Collection Name from the first html page. How can I achieve this?

This is my first html file:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Create New Collection</title>
<h><b>Create New Collection</b></h>
</head>
<body>
    <form method="post" action='CollectionPath' >
    <br>
    Collection Name:<textarea name="myCollectionName" cols="10" rows="1"></textarea>
    <br>
    <br>
    <input type="submit"
            value="Create" style="color:white;background: blue" />

    </form>
</body>
</html>

This is my second html file(index3.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action='CollectionPath' >

    Edit Collection: 
    <textarea name="CollectionNameValue" cols="10" rows="1"></textarea>
    <br>
    <br>
    <b>Include Content Matching the Following Patterns:</b>
    <br>

    <textarea name="pattern" cols="50" rows="10"></textarea>
    <br>
    example:http://www.mycompany.com/engineering/ 
    <br>
    <br>
    <b>Do Not Include Content Matching the Following Patterns</b>:
    <br>
    <textarea name="notPattern" cols="50" rows="10"></textarea>
    <br>
    example:http://www.mycompany.com/engineering/ 
    <br>
    <input type="submit"
            value="Save"  style="color:white;background: blue"/>


    </form>
</body>
</html> 
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rose
  • 1,490
  • 5
  • 25
  • 56

2 Answers2

2

The easiest answer is to make the second HTML page a JSP. Basically it will look similar to the existing index3.html, but will be renamed to something like "index3.jsp". Then, using simple JSP tags, you can get the data. First, though, you'll have to update your servlet:

protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");

    String CollectionName = request.getParameter("myCollectionName");
    // store this value in the session
    request.getSession().setAttribute("myCollectionName", CollectionName);
    String Pattern = request.getParameter("Pattern");
    String NotPattern = request.getParameter("NotPattern");
    response.sendRedirect("index3.jsp");
}

Now, in your JSP, you'll want to take a look at this posting for guidance on how to get the data from the session.

And a note on your code - tradition encourages you to use lower case (for single words) or camelCase for multi-word variable names. Java does not enforce this but it is encouraged.

EDIT

You mentioned in the comment that you'd like to only use servlets. But how are you serving your HTML files to the browser? At a guess you're using Tomcat or maybe Jetty. Those servers have the ability to handle JSP files too. The file would be in the same directory as you have it now but would be named differently. If you cannot use JSP you have some other choices, which are harder than JSP:

  1. Embed the contents of index3.html in the servlet. Basically you'll have a bunch of code that looks like the answer in this post and you'll place your string into the output. Issues: any changes to the page layout/color/wording require a recompile and redeploy.
  2. Create a "sentinel" value in index3.html like, for example, "----myvalue----". Read each line from index3.html and replace your sentinel value with the value you want then redirect to a new file you created during the replacement. Issues: what if you want two values? Now you need two sentinel values. What if you only have read permissions on the directory where the files you are serving are kept?
  3. Use JavaScript and pass a URL parameter to the second page. When it is rendered, pull the value from the URL and render it. Issues: now you've got two languages to deal with.

To be honest, all of these (and there are likely other ways too) are harder than the JSP way.

Community
  • 1
  • 1
stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • Thank you for your time. But is there a way to do it in Servlet? Because I have't use JSP before. – Rose May 17 '16 at 20:53
  • Use jsp with servlet has a lot of benefits such as using EL to access objects from Servlet. You can start using it by changing html extension to HTML. That's it. – Minjun Yu May 17 '16 at 21:07
  • Thanks @stdunbar I have used jsp approch and it is working. – Rose May 17 '16 at 21:43
2

Assumption

The path you specified in your form action tag is correct.

Here is a brief guide on how you do this:

Step 1

Change the extension of your two pages to jsp. e.g. index.html -> index.jsp. You will be able to use EL(Expression Language) in your jsp.

Step 2

In your Servlet:

protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
// Java's naming convention suggests that variable should be camel case
// e.g. String collectionName, please fix yourself.
String CollectionName = request.getParameter("myCollectionName");

request.setAttribute("collectionName", CollectionName);
//-- response.sendRedirect("index3.html");
// sendredirect will create a fresh request. As a result, the CollectionName you stored in the previous
// request does not exist anymore. You don't want that because your
// second page will get it from the request scope, see step 3.
// use forward instead
request.getRequestDispatcher("yourpage.jsp").forward(request,response);

// Not sure what these two lines are doing here because
// the previous html page do not have any input with name **Pattern**
// or "NotPattern", so you are not getting anything.
// please fix accordingly
String Pattern = request.getParameter("Pattern");
String NotPattern = request.getParameter("NotPattern");
}

Step 3

In your second page: Change yout textarea code to the following:

<textarea name="CollectionNameValue" cols="10" rows="1">${collectionName}</textarea>
<!-- you can do it because a String object with name **collectionName** is saved in request scope in the Servlet-->

Hope it helps.

Minjun Yu
  • 3,497
  • 4
  • 24
  • 39