Jsp-1 simple application servlet, and its jump page, jsp-1servlet
Servlet page Jump Method Used in jspweb
The jar package used only
Commons-lang3-3.5.jar
During running, tomcat first searches for the servlet based on the information in web. xml.
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>servlet</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>servlet</servlet-name> <servlet-class>com.javaweb.action.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
<Servlet> is the registered servlet and its physical address.
<Servlet-mapping> the relative address of servlet is how to use it in. jsp.
Then, wait for the user to operate according to index. jsp on the welcome page.
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% String path = request. getContextPath (); // get the current project root directory path String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path; // full path %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
Servlet response
Package com. javaweb. action; import java. io. IOException; import java. lang. reflect. invocationTargetException; import java. lang. reflect. method; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. commons. lang3.StringUtils; public class Servlet extends HttpServlet {/*** for Version Control */Private static final long serialVersionUID =-235792575087830020.l; @ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stub doPost (req, resp) ;}@ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method Stub // The chunk is used to determine whether there is an error String url = req. getServletPath (); String method = url. substring (1, url. lastIndexOf (". "); try {Method met = getClass (). getDeclaredMethod (method, HttpServletRequest. class, HttpServletResponse. class); try {met. invoke (this, req, resp);} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {e. printStackTrace () ;}} catch (NoSuchMethodException e) {e. PrintStackTrace ();} catch (SecurityException e) {e. printStackTrace ();} // use? Redirect page req. setCharacterEncoding ("UTF-8"); String op = req. getParameter ("op"); if (StringUtils. isNotBlank (op) {if ("second ". repeated signorecase (op) {second (req, resp);} else {PrintWriter out = resp. getWriter (); // call window
Out. println ("THIRD ");
}}} Public void first (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp. sendRedirect ("first. jsp ");} public void second (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp. sendRedirect ("second. jsp ");}}
Apparently used? Both the. do method and the. do method can implement the same function.
But when a large number of methods exist simultaneously? Methods can be used to differentiate different methods.
req.getParameter("login");
req.getParameter("logout");....
Result