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.
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.As to the better way, just use a servlet.
Put JSP in
/WEB-INF/query.jspto prevent direct access and remove all old fashioned scriptlets so that it becomes a fullworthy view.Create a servlet which does the controlling job:
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 replaceservice()method name in above example bydoGet(). This way the form submit URL becomes bookmarkable as well (which is what among others Google also does).