How the JSP submits the form to the corresponding servlet

Source: Internet
Author: User

Yesterday to learn these content, today do share it, personal feeling quite chaotic .... Oh, actually nothing, slowly just fine. Difficult not, will not be difficult! Effort + serious can be, I believe we can! Come on!!!


The following figure is the directory of the project I built with MyEclipse.


Directory structure a bit Doha ... It's really simple!

Here is all the code in my index.jsp: Here is just a form that is submitted to Testservlet.java in this servlet


?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <%@ page language="java" contentType="text/html; charset=GB18030"     pageEncoding="GB18030"%><meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title>                      <form action="servlet/TestServlet" method="post" dir="ltr">             <input name="userID" type="text"><br>             <input value="查询" type="submit"><br>         </form>             b.jsp       



Here is all the code in Testservlet.java: This is what gets the form submitted, and then forwards it to another page (c.jsp), where it contains a business logic (Usermanager.java)

Usermanager usermanager=new Usermanager ();
List Userlist=usermanager.finduserbyname (username);

These two lines of code are code for business logic to get the data.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package com.majianjie.servlet;import java.io.IOException; import java.util.List;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class TestServlet extends HttpServlet {    @Override     protected void service(HttpServletRequest req, HttpServletResponse res)             throws ServletException, IOException {                 //取得表单数据                 String username=req.getParameter("username");         UserManager usermanager=new UserManager();         List userlist=usermanager.findUserByName(username);                 req.setAttribute("userlist", userlist);//设置到req中,key-->value                  //转向到一个界面c.jsp,取出list中的数据         req.getRequestDispatcher("/a/b/http://blog.csdn.net/majianjie/article/details/c/c.jsp").forward(req, res);//转发getRequestDispatcher             }   }

Here is the line of code for Usermanager.java (business logic)

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.majianjie.servlet;import java.util.ArrayList; import java.util.List;public class UserManager {    public List<string> findUserByName(String name){         List<string> userList=new ArrayList<string>();         userList.add("数据一");         userList.add("数据二");         userList.add("数据三");         return userList;     } } </string></string></string>


Here is the code for C.JSP

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <%@ page language="java" contentType="text/html; charset=GB18030"     pageEncoding="GB18030"%>     <%@ page import="java.util.*" %><meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title>c.jsp     <p>     <%         List userList = (List)request.getAttribute("userlist");         out.println(userList);         %>     </p><p> a.jsp </p>


Here is the code for A.JSP

<%@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>







a.jsp





Here is the code for B.JSP


<%@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>






b.jsp




a.jsp

c.jsp

d.jsp

e.jsp





D.jsp, the code in e.jsp and the code in a.jsp, the only difference is: change A to D, change a to E ... Most of the code is generated automatically, it is very convenient.

Finally, the code in Web. XML: Here are some configuration information. Very IMPORTANT!!! Hey, heh.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!--?xml version="1.0" encoding="UTF-8"?--> <web-app version="2.5" 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_2_5.xsd">   <welcome-file-list>     <welcome-file>index.jsp</welcome-file>   </welcome-file-list>     <servlet>     <servlet-name>TestServlet</servlet-name>     <servlet-class>com.majianjie.servlet.TestServlet</servlet-class>   </servlet>     <servlet-mapping>         <servlet-name>TestServlet</servlet-name>         <url-pattern>/servlet/TestServlet</url-pattern>   </servlet-mapping>   </web-app>


In the form, when you click the Submit button, the link action= "Servlet/testservlet" is triggered. After the request arrives at server Tomcat, Tomcat checks all of the current project's web.
The property value in the configuration. See if either one is the same as the request address Servlet/testservlet. The results are found as follows:

Testservlet
/servlet/testservlet

Where/servlet/testservlet is your form's access address
Tomcat then finds and Testservlet based on the above
The corresponding configuration is as follows

Testservlet
Com.majianjie.TestServlet

The address of the Testservlet.java file is then found according to the.


You can then access it in the browser by setting up Tomcat. For example, I configured Tomcat to enter in the browser: http://localhost:8888/test_servlet/can be accessed,

Results


After clicking on the query:



:< yo after clicking the hyperlink b.jsp? http://www.2cto.com/kf/ware/vc/"target=" _blank "class=" Keylink ">vcd4kpha+pgltzybzcmm9" http://www.2cto.com/ Uploadfile/collfiles/20140225/20140225085659468.jpg ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.