JSP. 23 Ejemplo, Servlet definido en web.xml
En el proyecto actual o en uno nuevo vamos a
creamos un Servlet:
- New File. Categories: Web, File Types: Servlet. Click Next.
- Class Name: DisplayServlet, Package: donde queramos que se cree. Click Next.
- Seleccionamos Add information to deployment descriptor (web.xml).Servlet Name: DisplayServletURL Pattern(s): /display/*
El archivo WEB-INF/web.xml quedaría:
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>DisplayServlet</servlet-name>
<servlet-class>com.me.jsp.servlets.DisplayServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayServlet</servlet-name>
<url-pattern>/display/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>DisplayServlet</servlet-name>
<servlet-class>com.me.jsp.servlets.DisplayServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayServlet</servlet-name>
<url-pattern>/display/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Abrimos el servlet y hacemos uso de algunos de los
métodos de request para acceder a información de la petición:
package com.me.jsp.servlets;import java.io.IOException;import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DisplayServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DisplayServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet DisplayServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3>Métodos para el control de la URL</h3>");
out.println("<b>getServerName():</b> " +
request.getServerName() + "<br />");
out.println("<b>getContextPath():</b> " +
request.getContextPath() + "<br />");
out.println("<b>getServletPath():</b> " +
request.getServletPath() + "<br />");
out.println("<b>getPathInfo():</b> " +
request.getPathInfo() + "<br />");
out.println("<b>getMethod():</b> " +
request.getMethod() + "<br />");
out.println("<b>getRemotePort():</b> " +
request.getLocalPort() + "<br />");
out.println("<b>getRequestURI():</b> " +
request.getRequestURI() + "<br />");
out.println("<b>getRequestURL().toString():</b> " +
request.getRequestURL().toString() + "<br />");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// doGet y doPost invocan a processRequest
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet DisplayServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3>Métodos para el control de la URL</h3>");
out.println("<b>getServerName():</b> " +
request.getServerName() + "<br />");
out.println("<b>getContextPath():</b> " +
request.getContextPath() + "<br />");
out.println("<b>getServletPath():</b> " +
request.getServletPath() + "<br />");
out.println("<b>getPathInfo():</b> " +
request.getPathInfo() + "<br />");
out.println("<b>getMethod():</b> " +
request.getMethod() + "<br />");
out.println("<b>getRemotePort():</b> " +
request.getLocalPort() + "<br />");
out.println("<b>getRequestURI():</b> " +
request.getRequestURI() + "<br />");
out.println("<b>getRequestURL().toString():</b> " +
request.getRequestURL().toString() + "<br />");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// doGet y doPost invocan a processRequest
...
}
}
El siguiente link invocará al servlet:
<a href="display">display</a>
El resultado del servlet será:
getServerName():
localhost
getContextPath(): /CursoJSP
getServletPath(): /display
getPathInfo(): null
getMethod(): GET
getRemotePort(): 8084
getRequestURI(): /CursoJSP/display
getRequestURL().toString(): http://localhost:8084/CursoJSP/display
getContextPath(): /CursoJSP
getServletPath(): /display
getPathInfo(): null
getMethod(): GET
getRemotePort(): 8084
getRequestURI(): /CursoJSP/display
getRequestURL().toString(): http://localhost:8084/CursoJSP/display
El siguiente formulario invocará al servlet:
<form action="display/menu.jsp" method="POST">
<input type="submit" value="display/menu.jsp" />
</form>
<input type="submit" value="display/menu.jsp" />
</form>
El resultado del servlet será:
getServerName():
localhost
getContextPath(): /CursoJSP
getServletPath(): /display
getPathInfo(): /menu.jsp
getMethod(): POST
getRemotePort(): 8084
getRequestURI(): /CursoJSP/display/menu.jsp
getRequestURL().toString(): http://localhost:8084/CursoJSP/display/menu.jsp
getContextPath(): /CursoJSP
getServletPath(): /display
getPathInfo(): /menu.jsp
getMethod(): POST
getRemotePort(): 8084
getRequestURI(): /CursoJSP/display/menu.jsp
getRequestURL().toString(): http://localhost:8084/CursoJSP/display/menu.jsp
Comentarios
Publicar un comentario