Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hard-coding HTML element names

Tags:

html

jsp

servlets

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>
like image 235
user1154644 Avatar asked Mar 18 '23 04:03

user1154644


2 Answers

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.

like image 81
Santhosh Avatar answered Mar 26 '23 03:03

Santhosh


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/

like image 41
Habeeb Hassan Syed Avatar answered Mar 26 '23 02:03

Habeeb Hassan Syed