jsp v servlets.

Posted on 2005-04-12
Last Modified: 2010-04-01
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.  
Question by:annie613
    LVL 16

    Accepted Solution

    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:

    LVL 7

    Assisted Solution

    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.

    Author Comment

    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 :)

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Great sound, comfort and fit, excellent build quality, versatility, compatibility. These are just some of the many reasons for choosing a headset from Sennheiser.
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now