4

I know that when the name of an HTML form element changes, in order for the Servlet that is processing the form to retrieve the parameter, it has to be aware of the updated element name. In trying to reduce the amount of changes that have to be made (from 2 places to 1) I have created a static field in the Servlet that is referenced in the doPost() method at the time the parameter is retrieved, and also in the JSP, instead of hard-coding the element name. Can anyone think of a reason that this would be a bad idea, other than the use of a scriptlet? Should the name of the element need to change, I would now only have to change it in 1 place (the Servlet constant).

Servlet Code:

package com.example.servlets;

public class ServletDemo extends HttpServlet {
    public static final String FIRST_NAME_FIELD = "firstName"; 

    public void doPost(HttpServletRequest request, HttpServletResponse response){
        String firstName = request.getParameter(FIRST_NAME_FIELD);
        //do something with the first name
    }
}

JSP:

<%@ page import="com.example.servlets.ServletDemo" %>
<html>
  <head>
    <title>Example Page</title>
  </head>
  <body>
    <form method="POST">
      <h3>FirstName:</h3>
      <input name="<%=ServletDemo.FIRST_NAME_FIELD%>"/>
      <input type="submit" value="Submit"/>
    </form>
  </body>
</html>
Tiny
  • 27,221
  • 105
  • 339
  • 599
user1154644
  • 4,491
  • 16
  • 59
  • 102

3 Answers3

3

If i understood your question correctly you are currently using the scriptlets like below ,

<input name='<%= Attribute in request %>' , so that you can change it in the server side.

Would this be a bad idea to implement something like that? Thoughts?

It is not the problem with scriptlets as it can be replaced through EL or JSTL.If you are making all the input parameters in jsp name to be dyanamic. You need to pass the request to the servlet .

Consider even if the user deosn't submit the form to jsp , may be just viewing you need to send the data from the server (for the input parameter names)

So all your request should be intercepted by the servlet , though it is not required . So it may again create an overhead in the performance .

So i suggest you to add your idea in the pages where the URL is intercepted by the servlet in case you have many static pages.

Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • No, the name is not actually an attribute in the request, it is a reference to a static field in the Servlet class. My question was more of looking for a solution for having to change the name of html elements in 2 places. – user1154644 Oct 22 '14 at 18:34
  • @user1154644 I guess using a generic file for the constants is good idea , you could do that – Santhosh Oct 23 '14 at 05:50
2

Going by your Solution:

To set a variable and then to use it anywhere in the code using jstl and basic scriptlet.

<%@page import="com.foo.Constant"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="FOO_NAME" value="<%=Constant.FOO_FORM_FIRST_NAME_PARAMETER %>"/>

To access it through EL

<input name ="${FOO_NAME}" />

If you do not want to use the scriptlets there are other solutions as well.

How to reference constants in EL?

Other solutions

As I see it since it would anyways be a large application with many form and multiple inputs the more ideal solution is some form of binding input fields to.. say Pojo, that way the servlet does not have to keep track of the input names used in the form. That would probably lead to using a filter to capture the request parameters and save in some pojo and make it available in the request scope. OR To other web frameworks, like Spring mvc, Struts, JSF etc. If you think that using any of these frameworks is an overkill since you end up rewriting significant portions of the code, you may give the following a shot if you think Struts or Spring MVC is heavy:

https://code.google.com/p/microservlet/

Community
  • 1
  • 1
  • I understand your example, but in this case, setting the variable doesn't really do me any good in my opinion, since I am only referencing it in 1 place, I can just use the scriptlet directly in the name attribute. My question had more to do with whether or not there was any good reason to avoid using the static variable in the servlet and referencing it in the jsp, as opposed to hardcoding the element name, which seemed to be fairly common, with the name hardcoded in the JSP, and then have a constant in the servlet, but you still have to make 2 updates should the name of the attribute change. – user1154644 Oct 22 '14 at 18:51
2

If the same form is used by several classes and you have a different name in each class of the same form type then you might need to bind the form dynamically to all those classes. It means that you can create a dynamic binding where the actual binding is determined at runtime. But this usecase is rarely used in practice because it's more complex than static binding. And it's not the same like in your case where you create a constant for field name because you didn't use any javabean and its properties to get the actual field name that you want to bind with the parameter. And you don't like to use fields associated to the servlet class but instantiate some bean and populate that properties. For this case you might want to create some factory for bean instantiation and you need some constants to build a bean. But it seems far away from the topic of your question. If you used these constants as a keys to map some objects and can reuse that key on the view layer using some expression engine that's what you can achieve using that keys. The actual values might change at runtime and it gives you a level of abstraction because the flow also change dynamically. Make sure you have the flow that is properly configured. You should know how it's difficult to construct complex features using raw servlets instead of framework that make a life easier.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Now that I think about it, I guess it would make more sense to have that static field in a generic constants file, not necessarily bound to a particular servlet. – user1154644 Oct 23 '14 at 04:57
  • Whatever you like, in the earlier time constants are defined in interfaces and classes implemented those interfaces, nowadays you can use static imports. Sometimes I've used getters in java bean to get the value in JSP from the object in EL to not use static access. – Roman C Oct 23 '14 at 08:55