3
\$\begingroup\$

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?

\$\endgroup\$
5
  • \$\begingroup\$ Is your code broken or working? \$\endgroup\$ Commented Dec 17, 2015 at 10:16
  • \$\begingroup\$ Just to make sure: Is it or is it not working to the best of your knowledge? Is it doing what you want it to do? \$\endgroup\$ Commented Dec 17, 2015 at 10:16
  • \$\begingroup\$ Code is working and it's doing what I want it to do, but I'm not sure if there are cleaner or better ways to get the job done. \$\endgroup\$ Commented Dec 17, 2015 at 10:17
  • 2
    \$\begingroup\$ I suggest you should take a look at EL for reading values from Java beans in JSP. Because either a JSP tag or a scriptlet is too verbose, it leads less readable code. docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html Btw, why didn't you create the bean beanie in a servlet and pass it to JSP? So we can get rid of the significant amount of redundant code. \$\endgroup\$ Commented Dec 17, 2015 at 11:18
  • \$\begingroup\$ Thank you for the suggestion @HungTran. I'm not in the impression EL would be a better solution since it's just the evaluation and conversion of parameters. In contrast using a servlet instead of a "jsp-receiver" sounds pretty reasonable. Especially for beans with just a smaller functionality. I'd love to vote you up for that. \$\endgroup\$ Commented Dec 17, 2015 at 11:49

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.