-
Notifications
You must be signed in to change notification settings - Fork 0
Using Servlets to Generate Pages
A Java class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.
- Receive web (i.e., HTTP) requests
- In the case of a web application, request contains data submitted from HTML forms
- Process the request (logic, data model, database, other services)
- Respond with an HTTP response
- In the case of a web application, the response is HTML
NOTE: servlets are singletons Q: How does a web server handle multiple simultaneous requests? A: Threads Therefore: DO NOT store state (instance data) which should not be shared between requests
Data can be sent on the same line as the URL
- Another way to request a response from the server
- Data is sent as part of the request message itself
https://www.google.com/search?source=hp&ei=Ii1uWvb0HMG10ASQz7eYBQ&q=html5+doctype&oq=&gs_l=psy-ab.3.0.35i39k1l6.12164.13002.0.17705.5.4.0.0.0.0.329.329.3-1.2.0..2..0...1.1.64.psy-ab..3.2.1564.6..0j0i20i264k1j0i131i67k1j0i67k1j0i131k1.1235.yvXebgTk2Qw
POST / HTTP/1.1
Host: foo.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 13
say=Hi&to=Mom
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String html = "<html><body>Hi. I received param1=" +
request.getParameter("param1") +
" via POST</body><html>";
response.getOutputStream().println(html);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String html = "<html><body>Hi. I received param1=" +
request.getParameter("param1") +
" via GET</body><html>";
response.getOutputStream().println(html);
}package us.matt;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter; @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String user = request.getParameter("user");
if(user == null) user = Servlet.DEFAULT_USER;
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
writer.append("<!DOCTYPE html>\r\n")
.append("<html><head><title>Hello User</title></head>\r\n")
.append(" <body>\r\n")
.append(" Hello, ").append(user).append("!<br/><br/>\r\n")
.append(" <form action=\"greeting\" method=\"POST\">\r\n")
.append(" Enter your name:<br/>\r\n")
.append(" <input type=\"text\" name=\"user\"/><br/>\r\n")
.append(" <input type=\"submit\" value=\"Submit\"/>\r\n")
.append(" </form>\r\n")
.append(" </body></html>\r\n");
}
public class Servlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
this.doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.append("This is text!");
}
@Override
public void init() throws ServletException
{
System.out.println("Servlet " + this.getServletName() + " has started.");
}
@Override
public void destroy()
{
System.out.println("Servlet " + this.getServletName() + " has stopped.");
}
}@WebServlet(name = "Servlet",
urlPatterns = {"/greeting", "/salutation","/wazzup"},
initParameters = {
@webInitParam(name = "user", value = "Bob")
@webInitParam(name = "company", value = "Inc.")
}
)Get Parameter
String user = request.getParameter("user");
if(user == null)
user = Servlet.DEFAULT_USER;public class SimpleServlet extends HttpServlet Request and Response Objects
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {PrintWriter out = response.getWriter();
String docType = "<!DOCTYPE html>\n";
out.println(docType + "<html>\n); response.setContentType("text/html");http://site.com/simple?full_name=Bob&toppings=Olives&toppings=Anchovies&toppings=Mango
String fullName = request.getParameter("full_name");String[] paramValues =
request.getParameterValues("toppings");Enumeration paramNames = request.getParameterNames();List of HttpServletRequest parameter names
Enumeration paramNames = request.getParameterNames();while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();