Web Development using JSP & Servlet

Client-Server Architecture using Servlet:

<servlet>
<servlet-name>comingsoon</servlet-name>
<servlet-class>mysite.server.ComingSoonServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>comingsoon</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
@WebServlet(urlPatterns = "/helloworld")
public class HelloWorldExample extends HttpServlet {

private static final long serialVersionUID = 1 L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter printWriter = resp.getWriter();
printWriter.print("helloworld");
}
}
  1. init
  2. service
  3. destroy
  1. URL is used to send data.
  2. The request is not secured.
  3. The request is more efficient than Post.
  4. we can bookmark the URL.
  1. A large amount of data can be sent.
  2. Post is more secure.
  3. The request is less efficient.
  4. we can’t bookmark the URL.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.servlet.*;"%>  //directive tag
<% response.addDateHeader("Expires", 0); %>// scriptlet tag

<html><head><title>JSP</title></head>
<body><h1>Hello World!</h1>
<%! private int hits = 0; %>//declaration tag
You are visitor number <% synchronized(this) { out.println(++hits); } %>// scriptlet tag
since the last time the service was restarted.
<p>
This page was last updated: <%= new java.util.Date().toLocaleString() %>//expresion tag
</body></html>
import javax.servlet.*;//directive tag


public class HelloWorld2$jsp extends HttpJspBase {
private int hits = 0;//declaration tag

private static boolean _jspx_inited = false;

public void Service(HttpServletRequest request, HttpServletResponse response)// scriptlet tag
throws java.io.IOException, ServletException
{
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
String _value = null;
try {
if (_jspx_inited == false)
synchronized (this) {
if (_jspx_inited == false) {
_jspx_init();
_jspx_inited = true;
}
}
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
response.addDateHeader("Expires", 0);
out.write("\r\n<html><head><title>JSP</title></head>\r\n<body><h1>Hello World!</h1>\r\n");
out.write("\r\nYou are visitor number ");
synchronized(this) { out.println(++hits); }
out.write("\r\nsince the last time the server was restarted.\r\n<p>\r\nThis page was last updated: ");
out.print( new java.util.Date().toLocaleString() );//expresion tag
out.write("\r\n</body></html>");

} catch (Throwable t) {
if (out != null && out.getBufferSize() != 0) out.clearBuffer();
if (pageContext != null) pageContext.handlePageException(t);
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
}
}
}
  1. Directive tag:- This tag is used to import, include anything to that current jsp page, and also with the help of this tag we can use some external tag also. By using <% ……%> this syntax on the JSP page we can do the above mention task.
  • <%@ page … %>: page tags use to specifies language, session, errorPage,import autoFlush etc.
  • <%@ include … %>: include is used to include page.
  • <%@ taglib … %>: Taglib is used to specifies the tag libraries.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store