• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

jsp v servlets.

hello..i was hoping to get some suggestions about servlets and jsp.

i first starting learning servlets so i would be able to use jsp
and now after playing around with servlets i am comfortable.
i took my servlet code that i have been working with and basically
put it in a big scriplet and i realize that is not the proper thing to do...

the servlet runs well and so does the jsp...

but i was hoping to get some suggestions on cleaning up
the code...i read an article that using some HTML and
standard JSP variable are better for jsp pages...any comments????

i guess i am asking for some help with jsp features??? any suggestions????

thanks in advance!!!!!!
annie :)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

            <title>Adding Names To A Session</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <link   rel=stylesheet

      <%@ page import="java.util.*, coreservlets.*" %>

      <%-- my servlet code i added to the jsp page --%>
      <%-- adding a name from a URL query string--%>
      HttpSession currentSession;
      String newName;
      Vector sessionNames;
      Integer sessionCount;

      currentSession = request.getSession(true);

      newName = request.getParameter("name");
      sessionNames = (Vector)currentSession.getAttribute("users");
      sessionCount = (Integer) currentSession.getAttribute("count");

      if(sessionNames == null)
            sessionNames = new Vector();
      }//end if

      currentSession.setAttribute("users", sessionNames);
      Integer tempInteger = new Integer(sessionNames.size());
      currentSession.setAttribute("count", tempInteger);      

      <h1 align="center">Adding Names To A Session</H1>

            <p>A New Name Has Been Added!</p>
            To see the entire list, visit the
            <a href="http://localhost:8080/ShowNames.jsp">
            <code>ShowNames</code></a> JSP page.  
2 Solutions
I don't see anything wrong with the JSP.  The advatage with JSP is the ability to put HTML directly into the code. In order to do this in a servlet you would need to obtain the PrintWriter and do out.println's for the HTML you wished to produce.  This can be very cumbersom.  

JSP's also give you the ability to use JSP taglibs.  There are a ton of them out there for different things.  This enables you to start using XML tags to replace some of your inline scripting in the JSP, which in my opinion makes the JSP even easier to read.  They are hard to wrap your mind around if you are used to the inline scripting, but after you get used to them , you would not want to turn back.

If you are interested, here is a good site for taglibs:


first thing u must remember while using jsp and servlets is that jsp is designed for use in presentation layer.
though u can achieve the same result in jsp and servlet, it is always best to put the business logic in servlets and leave the  presentation in jsp.

some common mistakes done by ppl are to keep the html tags inside the code while using jsp
<% out.print("<h1>" + var +"</h1>"); %>
though this will work fine and achieve the desired result it is always preffered to keep html tags out in jsp
so the above example shud be
<h1><% out.print(var); %></h1>
this is best practice as it improves readability

then comes JSP taglibs. since  "JoeZ430" has already said abt it, i am not going deep with it.
annie613Author Commented:
thanks for the insight...i see now why jsp is used for presentation..and servlets for processing...i was confused on how to call the java code...directly or indirectly....i know this program is small so directly would work but i wanted to get in the habit of calling the code indirecly because i will be working more amounts of code in the future. cheers to you both :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now