0

I would like to write a simple form in JavaEE. To do that I've created a simple form.jsp file. The code of this file is given below:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>

    <% if(request.getAttribute("action")!=null) {
            String action=(String) request.getAttribute("action");
            if(action.equals("success")) { %>
                <br>Added user correctly.</br>      
         <% } else { %>
                <br>Error : <%=action %></br>   
         <% } 
        } else { %>
        <u>Rejestracja:</u>
        <form action="servletAgent" method="post">
            <fieldset>
                <table style="text-align: right;">          
                    <tr><td>imie: </td><td><input type="text" style="width:220px" required="required" name="name" value=""></td></tr>               
                </table>
                <center><input type="submit" value="Register"/></center>
            </fieldset>
        </form>
     <% } %>

</body>
</html>

In this code we can see that I connect to "servletAgent". In "servletAgent.java" there is a method doPost(), where I check if the input name is correct if yes the information "Added user correctly" is displayed in other way the "Error" is displayed. Is there any other possiblity to do that, I mean is this method good to connect to servletAgent, maybe there is possiblity not to connect to servletAgent and do that in form.jsp file without any connection to servletAgent simplier?

user2856064
  • 541
  • 1
  • 8
  • 25

1 Answers1

0

maybe there is possiblity not to connect to servletAgent and do that in form.jsp file without any connection to servletAgent simplier?

You can handle all the work directly in the JSP, just change the action from "servletAgent" to "form.jsp" and now your JSP will be able to also evaluate the request, then you can move the code from the servlet to the top of your scriptlet code. Technically, it will work as you expect and you won't need any servlet to handle your business logic at all...

BUT!

Using scriptlets as you're doing in this page is not the best option mainly for maintenance reasons (there are more others, but I will focus on this one). When you implement the MVC pattern, basically by decoupling the JSP from the business logic, you accomplish maintainability. For example, a Web UI Designer can apply any HTML5/JS/CSS he/she wants directly on an (almost) pure HTML page regardless the file extension, and the programmer can address business logic in the servlet or another server side component like Managed Beans in JSF or @Controllers in Spring MVC. Looks like more work to do, and it is when working with JSP and Servlets directly, but that's why there are several Java Web Application frameworks: to ease this maintenance.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332