Search code examples
javaweb-servicesjspservletsrestlet

JSP client to test Servlet-deployed RESTful webservice


I recently used the restlet.org framework to create a simple servlet-deployed webservice (using Tomcat) in Eclipse with which I can show text from a textfile in a browser. I also created a JSP (input.jsp) with a simple form: one textbox and one button. When a user enters a word and clicks on the button, a second JSP (result.jsp) should open showing only the phrases of the text file containing that word.

I'd like to build a testclient using these JSP's to test my webservice, but I don't really know how to go about this. Both of these JPS's are currently located in the /WEB-INF folder of my project, but if I try to run the first JSP on my server, an error pops up saying: "The server has not found anything matching the request URI". Simply navigating to the URI's defined in the QuotesApplication class below works perfectly fine.

Application class:

package edu.ap.rest;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

public class QuotesApplication extends Application {

    /**
     * Creates a root Restlet that will receive all incoming calls.
     */
   @Override
   public synchronized Restlet createInboundRoot() {

       Router router = new Router(getContext());

       router.attach("/quotes", QuotesResource.class);
       router.attach("/quotes/{searchTerm}", QuotesSearchResource.class);
       return router;
   }
}

Resource 1:

package edu.ap.rest;

import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

import edu.ap.txt.TXTParser;

public class QuotesResource extends ServerResource {

    @Get("html")
    public String getQuotes() {
        TXTParser parser = new TXTParser();
        return parser.getQuotes();
    }

}

Resource 2:

package edu.ap.rest;

import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

import edu.ap.txt.TXTParser;

public class QuotesSearchResource extends ServerResource {

    @Get("html")
    public String getSearchQuotes(String searchTerm) {
        searchTerm = getAttribute("searchTerm");
        TXTParser parser = new TXTParser();
        return parser.getSearchQuotes(searchTerm);
    }
}

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app id="WebApp_ID" version="2.4"  
            xmlns="http://java.sun.com/xml/ns/j2ee"  
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
                 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
   <display-name>first steps servlet</display-name>

   <!-- Application class name -->
    <context-param>
    <param-name>org.restlet.application</param-name>
    <param-value>
        edu.ap.rest.QuotesApplication
    </param-value>
    </context-param>

   <!-- Restlet adapter -->  
   <servlet>  
      <servlet-name>RestletServlet</servlet-name>  
      <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
   </servlet>  

   <!-- Catch all requests -->  
   <servlet-mapping>  
      <servlet-name>RestletServlet</servlet-name>  
      <url-pattern>/*</url-pattern>  
   </servlet-mapping>

</web-app>

Input JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Zoek een quote</title>
</head>
<body>

  <form method="POST" action="Result.jsp">
    Zoekterm: <input type="text" name="searchTerm"><br/>
    <br/><br/>
    <input type="submit" value="Submit">
  </form>

</body>
</html>

Result.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="edu.ap.rest.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Resultaat</title>
</head>

<% 
	QuotesSearchResource resource = new QuotesSearchResource();
	String searchTerm = ("searchTerm");
	System.out.println("Test: " + searchTerm);
	String Result = resource.getSearchQuotes(searchTerm);
%>
<body>
<%= Result %>
</body>
</html>


Solution

  • JSP is a servlet too. You're missing the section like this:

    <servlet>
        <servlet-name>QuoteInputServlet</servlet-name>
        <jsp-file>/Input.jsp</jsp-file>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>QuoteInputServlet</servlet-name>
        <url-pattern>/input</url-pattern>   
    </servlet-mapping>