A goal of this script is to tutor you how to develop the servlets that are part of a Java web application.
In contrast to the JSP, the servlet is a Java class that runs on a server and does the processing for the energetic web pages of a web application. That’s why servlets for a web application are written by web programmers, not web designers. After the processing is done, the servlet returns HTML code to the browser by using the println method of an out object.
Note, however that this makes it more difficult to code the HTML.
In case you’re interested, each servlet for a web application is a Java class that extends (or inherits) the HttpServlet class. Then, each servlet can override the doGet method of the inherited class, which receives both a request and a response object from the web server, and the servlet can get the parameters that have been passed to it by using the getParameter method of the request object.
After that, the servlet can do whatever processing is required by using normal Java code. Before this, you have to understand the details on how servlets work. When you complete that, you’ll be able to write significant servlets of our own. But as you’ll learn next, the real trick as you develop Java web applications is to use a combination of servlets and JSPs so you get the benefits of both.
How to combine servlets and JSPs in a web application
As you have seen, servlets are actually Java classes so it makes logic to use them for the processing requirements of the web pages in an application. Similarly, JSPs are primarily HTML code so it makes logic to use them for the design of the web pages in an application.
But how can you do that in an efficient way?
The solution is for a servlet to do the processing for each web page, and then forward the request and response objects to the JSP for the page. That way, the servlet does all the processing, and the JSP does the entire HTML. With this approach, the JSP requires a minimum of embedded Java code. And that means that the web designer can write the JSPs with little or no help from the Java programmer, and the Java programmer can write the servlets without worrying about the HTML.
To use above approach, you have to learn how to use this approach for developing web applications. And also learn how to use the Model-Controller-View pattern to structure your applications so they’re easy to manage and maintain. When you finish that, you’ll know how to develop Java web applications in a thoroughly professional manner.
Example
Partial code for a servlet that works the same as the JSP
public class EmailServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
out.println(
"<html>\n"
+ "<head>\n"
+ " <title> Email List application</title>\n"
+ "</head>\n"
+ "<body>\n"
+ "<h1>Thanks for joining our email list</h1>\n"
+ "<p>Here is the information that you entered :</p>\n"
+ " <table cellspacing=\"5\" cellpadding=\"5\" border=\"1\">\n"
+ " <tr><td align=\"right\">First name :</td>\n"
+ " <td>" + firstName + "</td>\n"
+ " </tr>\n"
+ " <tr><td align=\"right\">Last name:</td>\n"
+ " <td>" + lastName + "</td>\n"
+ " </tr>\n"
+ " <tr><td align=\"right\">Email address:</td>\n"
+ " <td>" + emailAddress + "</td>\n"
+ " </tr>\n"
+ " </table>\n"
+ "</html>);
Description
1) A servlet is a Java class that runs on a server. Although servlets are commonly used for web applications, they can also be used for other types of applications like mail or FTP server applications.
2) A servlet for a web application extends the HttpServlet class. This makes it easy for a Java programmer to write the code for the processing that a web page requires. Once a servlet is compiled, it can be run by the servlet engine.
3) To return HTML code to the browser, a servlet uses the println method of an out object. This makes it more difficult to write the HTML portion of the code.
4) To get the best results from servlets and JSPs, you use a combination of the two as you develop web pages. In particular, you use servlets for the processing that’s required by the pages, and JSPs for the HTML that’s required by the pages.


LinkBack URL
About LinkBacks
Reply With Quote
LinkBacks Enabled by vBSEO
Bookmarks