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>