JSP syntax to switch between HTML and XML
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 TEXTARE开发者_JS百科A 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.
<% 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).
精彩评论