0

In Below code JDBC connection is done in JSP file itself. What the harm in that. I am new to this and finding in most of the sites its asked to use JSTL for SQL data, what is the difference between JSTL and JSP then and which one is better?

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
    <title>Home Page</title>
    </head>
    <body>
       <%@include file="DBDetails.jsp"%>   
<%
 Class.forName("com.mysql.jdbc.Driver").newInstance();
                          connection = DriverManager.getConnection(connectionURL, username, password);
                            statement = connection.createStatement();

Code TO get Table Values Et

%>
    <%User user = (User) session.getAttribute("User"); %>
    <h3>Hi <%=user.getName() %></h3>
    <strong>Your Email</strong>: <%=user.getEmail() %><br>
    <strong>Your Country</strong>: <%=user.getCountry() %><br>
    <br>
    <form action="Logout" method="post">
    <input type="submit" value="Logout" >
    </form>
    </body>
    </html>

1 Answers1

1

This violates the first SOLID design principle of object oriented programming, which states that each class should have a single responsibility. JSPs are compiled into classes, so it holds that JSPs should also have a single responsibility. This simple toy JSP page is responsible for:

  1. Printing HTML
  2. Connecting to a database
  3. Querying the DB
  4. Handling database errors
  5. Populating additional Java objects with data from the database

In addition, JSTL is one of several technologies that help developers avoid doing what you're doing here - writing Java code within their JSP files. This was common practice in the late 1990's and early 2000's, but it's considered bad practice for anything other than hobby projects.

Michael Peacock
  • 2,011
  • 1
  • 11
  • 14
  • Thanks for the ans, by Reading your answer I could only figure out using java code snipped in JSP is older technology and JSTL is the latest trend which is being followed, so on the similar lines Can you please explain more on JSTL or provide me any reference where in I can go read more of JSTL.... – arjunhitachi Apr 24 '18 at 14:41
  • @arjunhitachi JSTL is hardly "the latest trend" (the first version is from 2002), and just like JSP it is considered old and outdated. And JSTL is associated with JSP, it is not as if using JSTL means you are not using JSP, and if you are going to use JSP, then you really should use JSTL. – Mark Rotteveel Apr 24 '18 at 14:52
  • ok thank you so much...Last question, your latest answer says neither JSP nor JSTL is new, then which one is new market which I should go for? I am trying to learn MVC and Struts...for which new tech I should go for and how much time might take? – arjunhitachi Apr 24 '18 at 14:55
  • I would suggest Spring Boot, with Spring Web MVC, and Thymeleaf as a good starting point. You can configure a working application template using the Spring initializer here: https://start.spring.io/ – Michael Peacock Apr 24 '18 at 15:00
  • `JSP` has been replaced by `JSF` (Java server faces). `Spring MVC` is an alternative technology but it still needs `Thymeleaf` or even `JSP` for the presentation (view) layer. – dsp_user Apr 25 '18 at 08:33