0

I would like to have a single JSP page that will do the following:

  • If the method is GET and the querystring is NULL, draw a HTML form with a TEXTAREA and SUBMIT button
  • If the method is GET and the querystring is not NULL or the method is POST, genete an XML document using GET/POST variables

My first approach draft (test POST or GET) fails syntactically:

query.jsp

<%@ page import="..." %>
<%!
  private void to_xml() {
  ...                
  }
%>
<% if (request.getMethod()="POST") { %>

<?xml version="1.0" encoding="UTF-8"?>

<%
  //generate XML 
  to_xml();
} 

else { 

  //draw HTML form
%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>XML Query</title>
    </head>
    <body>
        <form action="query.jsp" method="post">
            <table cellpadding="2">
                <tr><td>Query:</td></tr>
                <tr><td><textarea name="query" cols="60" rows="10" ></textarea>
                <tr><td><input type="submit" value="Go"></td></tr>
            </table>
        </form>
    </body>
</html>

<% } %>

I'm sure that there is a better way to do this, but my experience w/ JSP is limited.

craig
  • 25,664
  • 27
  • 119
  • 205

1 Answers1

0

As to your concrete problem: in Java, strings are objects, not primitives. You need to compare objects by the Object#equals() method, not by equality operator == (or even more incorrectly, the assignment operator =). This is not different when writing raw Java code straight in a JSP file instead of a Java class.

<% if (request.getMethod().equals("POST")) { %>

As to the better way, just use a servlet.

Put JSP in /WEB-INF/query.jsp to prevent direct access and remove all old fashioned scriptlets so that it becomes a fullworthy view.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>XML Query</title>
    </head>
    <body>
        <form action="query" method="post">
            <table cellpadding="2">
                <tr><td>Query:</td></tr>
                <tr><td><textarea name="query" cols="60" rows="10" ></textarea>
                <tr><td><input type="submit" value="Go"></td></tr>
            </table>
        </form>
    </body>
</html>

Create a servlet which does the controlling job:

@WebServlet(urlPatterns={"/query"})
public class QueryServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (request.getParameterMap().isEmpty()) {
            showJsp(request, response);
        } else {
            showXml(request, response);
        }
    }

    private void showJsp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/query.jsp").forward(request, response);
    }

    private void showXml(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // ...
    }

}

Open the page by http://localhost:8080/contextname/query.

I however wonder how it's useful to mix POST/GET here since you want to intercept on the query string as well. Just remove method="post" from the <form> and replace service() method name in above example by doGet(). This way the form submit URL becomes bookmarkable as well (which is what among others Google also does).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • After reading your response, some contemplation, and some research, it appears what I really want is RESTful interface to Java. While I could use Jersey or RESTEasy, perhaps implemented selected bits of JAX-RS will meet my needs. – craig Jun 14 '11 at 20:33
  • That was also what I initially thought :) It's only another learning curve. Note that Jersey and RESTEasy are both concrete JAX-RS API implementations. Like as that Tomcat is a concrete JSP/Servlet API implementation. – BalusC Jun 14 '11 at 20:35
  • I really only have two classes: Session and InfoObjects (BusinessObjects). I need GET/POST/DELETE for Session (auth form/authenticate/logout) and GET for InfoObjects (get one object or multiple). While I'm sure these frameworks would make this easy, I'm leary of adding additional complexity. Is there a less-complex way? – craig Jun 14 '11 at 20:54
  • Well, using a plain servlet is less complex. Basically, what I've put in my answer is everything you need. But using JAX-RS will give you way much more flexibility with regard to future changes and expansions. Note that JAX-RS is designed on top of the basic Servlet API. So, it is under the covers using a plain servlet, but just with all potential boilerplate/repetitions abstracted away so that you don't need to Ctrl+C/Ctrl+V the entire JSP/Servlet code, but, say, just add one more method. – BalusC Jun 14 '11 at 20:56
  • Is there a way to pass a variable defined int the Servlet to the view? Most of the examples that I've seen dynamically create the view. I would like to be able to use something the <%= XXX %> syntax. – craig Jun 20 '11 at 00:39
  • 1
    *Scriptlets* are discouraged since a decade: http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files. Just set an attribute in request scope like `request.setAttribute("foo", foo)` and access it in the forwarded JSP by EL like `${foo}`. See also http://stackoverflow.com/tags/servlets/info and http://stackoverflow.com/tags/el/info. – BalusC Jun 20 '11 at 00:51