views:

234

answers:

2

As you can see here that I have a text box with three submit buttons each redirecting to a different jsp page, however in those jsp pages, when I do request.getParameter("bid"), all I get is null... How can I fix this the simplest way possible?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>View/Modify Student Information</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<script type="text/javascript" src="scripts/search.js"></script>
</head>
<body>
<br/>
<div align="center">
<h2>View/Modify Student Information</h2>
<label> Student Banner ID: </label>
<input type="text" name="bid" />
<br/>
<br/>
<form method = "post" action="rent.jsp">
<input type="text" name="bid1" />
<input type="submit" value="Total Rent" onClick="goto_rent()"/>
</form>
<form method = "post" action="adviser.jsp">
<input type="text" name="bid2" />
<input type="submit" value="Adviser Information" onClick="goto_adviser()"/>
</form>
<form method = "post" action="delete_std.jsp">
<input type="text" name="bid3" />
<input type="submit" value="Delete Student" onClick="goto_del()"/>
</form>

</div>
</body>
</html>
A: 

when I do request.getParameter("bid"), all I get is null...

Because it is not placed inside the same form. Just have it all in one form. To distinguish the button pressed, just give each the same name. Best way is to create a servlet which takes action accordingly based on the button pressed:

<form action="studentServlet" method="post">
    <input type="text" name="bid" />
    ...
    <input type="submit" name="action" value="Total Rent" />
    <input type="submit" name="action" value="Adviser Information" />
    <input type="submit" name="action" value="Delete Student" />
</form>

The com.example.StudentServlet class should look like:

package com.example;

import java.util.HashMap;
import java.util.Map;

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

public class StudentServlet extends HttpServlet {

    private static final Map<String, String> pages = new HashMap<String, String>() {{
        put("Total Rent", "/WEB-INF/rent.jsp");
        put("Adviser Information", "/WEB-INF/adviser.jsp");
        put("Delete Student", "/WEB-INF/delete_std.jsp");
    }};

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        String action = request.getParameter("action");
        String page = pages.get(action);
        request.getRequestDispatcher(page).forward(request, response);
    }

}

Map this servlet in web.xml on an url-pattern of /studentServlet (or so, at least the form action must point to it). The JSP pages are by the way located in /WEB-INF to prevent direct access without an intermediating Servlet.

This way you can in each of the JSP pages access the bid parameter by

<p>Your bid was ${param.bid}</p>

Hope this helps.

BalusC
is there any other simpler way besides this? as I don't know what com.example.StudentServlet is....
Aditya H
I want to use the request.getParamater("bid") in my jsp code not <p>Your bid was ${param.bid}</p>
Aditya H
1) It is a Java class extending `javax.servlet.http.HttpServlet`. I've edited the code example to demonstrate more. 2) It does exactly the same, but then using Expression Language instead of the old fashioned scriptlets which is discouraged more than 10 years ago. I strongly recommend to go through a decent JSP/Servlet tutorial so that you get somewhere.
BalusC
A: 

sorry but I still wish to use getParameter

Aditya H.