Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • 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">

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

      <%@ 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

      sessionNames.addElement(newName);
      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.  
      </body>
</html>
0
annie613
Asked:
annie613
2 Solutions
 
JoeCommented:
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:

http://jsptags.com/tags/index.jsp

Joe
0
 
maXXXeECommented:
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
eg:
<% 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.
0
 
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 :)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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