I was never really sure what the cleanest way would be to use JSP-Pages for receiving sent parameters. It's common knowledge to use as little JSP as possible - yet as some things have to be done there, for example the request.getParameter-Commands.
Right now most of my pages which are fetching parameters look like that, but I'm still not sure if this is clean enough:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%!
int id=0;
double price = 0;
%>
<%
String article = request.getParameter("article");
try {
price = Double.parseDouble(request.getParameter("price"));
} catch (NumberFormatException nufo) {
} catch (NullPointerException nupo) {
}
try {
id = Integer.parseInt(request.getParameter("id"));
} catch (NumberFormatException nufo) {
} catch (NullPointerException nupo) {
}
String user = session.getAttribute("user").toString();
%>
<jsp:useBean class="mybean.UsedBean" id="beanie" scope="page">
<jsp:setProperty name="beanie" property="article" value="<%=article%>"/>
<jsp:setProperty name="beanie" property="id" value="<%=id%>"/>
<jsp:setProperty name="beanie" property="price" value="<%=price%>"/>
<jsp:getProperty name="beanie" property="update"/>
</jsp:useBean>
setArticle, setId, setPrice are just regular setters while getUpdate is the method which actually "does" something and returns either true/false, the number of affected lines in the database or some sort of string.
Yet I doubt if this could be done better.
Skipping the receiving-part and directly forwarding the parameters to the fields via:
<jsp:setProperty name="bean" property="para" value="<%=request.getParameter("para")%>"/>
Attribute value request.getParameter("para") is quoted with " which must be escaped when used within the value is causing an exception:
> org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jsp] in context with path [/Project] threw exception [/project/GetForm.jsp (line: 70, column: 77) Attribute value request.getParameter("para") is quoted with " which must be escaped when used within the value] with root cause
> org.apache.jasper.JasperException: /project/GetForm.jsp (line: 70, column: 77) Attribute value request.getParameter("para") is quoted with " which must be escaped when used within the value
This question has been asked already several times but I'm not sure if I want to "have it solved".
I don't want to have int or double fields in my bean which have String-setters.
Also having a setRequest method in every bean does not really seem to be the best possible way either.
So for my purposes - is the way I'm using the JSPs correct? Or what would be better?