tags:

views:

40

answers:

3

Is there a way to have multiple links go to one blank page, but the each of the different links display a seperate query on the page instead of having seperate pages to do it, or am I asking to much. I am do this with JSP pages. Any help would be appreciated.

Thanks.

+3  A: 

Yes. Using GET or POST variables you update the display of a page based on the arguments passed:

/page.jsp?arg1=val1&arg2=val2
/page.jsp?arg1=val2

Then the page.js code:

String arg1 = request.getParameter("arg");
String arg2 = request.getParameter("arg2");

if (arg1 == "val1")
  // Do something
if (arg2 == "val2")
  // Do something else
pygorex1
A: 

There is a file web.xml at war/WEB-INF.

You can enter the following in the file.

<servlet-mapping>
    <servlet-name>myApp1</servlet-name>
    <url-pattern>/hello/dolly</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>myApp2</servlet-name>
    <url-pattern>/bye/bunny</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>myApp1</servlet-name>
    <servlet-class>
    misc.apps.enter.theDragon
    </servlet-class>
</servlet>
<servlet>
    <servlet-name>myApp2</servlet-name>
    <servlet-class>
    misc.apps.enter.theDragon
    </servlet-class>
</servlet>

Here is the explanation:

  1. Define a servlet identifier myApp1. Define the context-relative url pattern/path of myApp1 as /hello/dolly
  2. Similarly, for identifier myApp2, define context-relative url pattern/path as /bye/bunny
  3. Then associate myApp1 to the servlet misc.apps.enter.theDragon.
  4. As well as, associate myApp1 to the servlet misc.apps.enter.theDragon.

So, now both paths

  1. /hello/dolly
  2. /bye/bunny

actually end up calling the same servlet misc/apps/enter/theDragon.java

Then inside the servlet code misc/apps/enter/theDragon.java you have the code

this.requestUri = request.getRequestURI();
if (this.requestUri.contains("/hello/dolly")){
  happyRoutine();
else // if (this.requestUri.contains("/bye/bunny"))
{
  response.redirect("whatever page");
}

But, you want to map a jsp not a non-jsp servlet, don't you? Take a look at the web.xml entries below and I am sure you do not need any further explanation of how two url paths is being mapped to the same jsp. Then, inside the jsp, again use request.getRequestURI() to find out which url was used to invoke the jsp and then make the respective actions you wish to make.

<servlet-mapping>
    <servlet-name>empJSP1</servlet-name>
    <url-pattern>/hello/dolly</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>empJSP2</servlet-name>
    <url-pattern>/bye/bunny</url-pattern>
</servlet-mapping>

<servlet>
  <servlet-name>empJSP1</servlet-name>
  <jsp-file>/employees/emp.jsp</jsp-file>
</servlet>
<servlet>
  <servlet-name>empJSP2</servlet-name>
  <jsp-file>/employees/emp.jsp</jsp-file>
</servlet>
Blessed Geek
+1  A: 

There the Servlet is for. You can make use of it to control, preprocess and postprocess requests to a high degree. You can make use of query string to pass request specific information to the servlet, e.g. http://example.com/context/servlet?name1=value1&amp;name2=value2, which can be accessed in Servlet as follows:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name1 = request.getParameter("name1"); // Now contains "value1".
    String name2 = request.getParameter("name2"); // Now contains "value2".
    // Do your business thing with them.
}

You can also make use of request pathinfo to pass request specific information to the servlet, this results in more nicer URL's, e.g. http://example.com/context/servlet/value1/value2, which can be accessed in Servlet as follows:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String pathInfo = request.getPathInfo(); // Now contains "/value1/value2".
    // Do your business thing with them.
}

In both cases the Servlet is of course mapped in web.xml as follows:

<servlet>
    <servlet-name>servlet</servlet-name>
    <servlet-class>com.example.Servlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>servlet</servlet-name>
    <url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

To display results in JSP, you need to store the data in the request scope and forward the request to a JSP for display. E.g.:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String query = request.getParameter("query");
    List<Result> results = searchDAO.list(query);
    request.setAttribute("results", results);
    request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
}

Here the Result class is just an ordinary JavaBean class roughly representing one row of the database table. The JSP is placed in /WEB-INF to prevent from direct access by URL. You of course want that only the servlet can access it. In JSP you can in turn use Expression Language to access any scoped attributes, such as results from the above example. You can also use taglibs in JSP to control the page flow and output. A well known standard taglib is JSTL (just drop jstl-1.2.jar in /WEB-INF/lib to get it to work), here's an example how to display results nicely in a JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

...

    <table>
        <tr>
            <th>Name</th>
            <th>Description</th>
        </tr>
        <c:forEach items="${results}" var="result">
            <tr>
                <td>${result.name}</td>
                <td>${result.description}</td>
           </tr>
        </c:forEach>
    </table>

To learn more about JSP/Servlets, I can recommend the Marty Hall's Coreservlets.com tutorials. To learn more about interacting with databases the right way (the DAO pattern), you may find this article useful as well.

Good luck.

BalusC