I learned these things yesterday. Let's share them today. I feel quite confused .... Well, it's actually okay. Just take it easy. No, it's not hard! Work hard and be careful. I believe everyone can do it! Come on !!!
The following figure shows the directory of the project I created using myeclipse.
The directory structure is a bit Doha... It's actually easy!
Below is all the code in my index. jsp: there is only a form that is submitted to the servlet TestServlet. java.
<% @ Page language = "java" contentType = "text/html; charset = GB18030" pageEncoding = "GB18030" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
The following is all the code in TestServlet. java: This is used to obtain the content submitted by the form and forward it to another page (c. jsp), which contains a business logic (UserManager. java)
UserManager usermanager = new UserManager ();
List userlist = usermanager. findUserByName (username );
The above two lines of code are the business logic code to get data.
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 {@ Overrideprotected void service (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {// get form data String username = req. getParameter ("username"); UserManager usermanager = new UserManager (); List userlist = usermanager. findUserByName (username); req. setAttribute ("userlist", userlist); // set to req, key --> value // switch to an interface c. jsp to retrieve the data req in the list. getRequestDispatcher ("/a/B/c. jsp "). forward (req, res); // forward getRequestDispatcher }}
The following is the code line of UserManager. java (business logic)
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 ("data 1"); userList. add ("Data 2"); userList. add ("data 3"); return userList ;}}
Below is the c. jsp code
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ page import="java.util.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
The following is the code of a. jsp.
<% @ Page language = "java" contentType = "text/html; charset = GB18030"
PageEncoding = "GB18030" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GB18030">
<Title> Insert title here </title>
</Head>
<Body>
A. jsp
</Body>
</Html>
The following is the B. jsp code.
<% @ Page language = "java" contentType = "text/html; charset = GB18030"
PageEncoding = "GB18030" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GB18030">
<Title> Insert title here </title>
</Head>
<Body> B. jsp
<P>
<A href = ".../a. jsp"> a. jsp </a> <br>
<A href = "c/c. jsp"> c. jsp </a> <br>
<A href = "../d. jsp"> d. jsp </a> <br>
<A href = ".../../e. jsp"> e. jsp </a> <br>
</Body>
</Html>
D. jsp, e. code in jsp and. the code in jsp is the same. The only difference is: CHANGE a to d and change a to e ...... most of the Code is automatically generated, which saves a lot of trouble.
Finally, the Code in web. xml: Here is some configuration information. Very important !!! Hey
<?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 the tomcat server, tomcat checks all <servlet-mapping> </servlet-mapping> in the web. xml of the current project.
The attribute value of <url-pattern> </url-pattern> in the configuration. Check whether one of them is the same as the request address servlet/TestServlet. The result is as follows:
<Servlet-mapping>
<Servlet-name> TestServlet </servlet-name>
<Url-pattern>/servlet/TestServlet </url-pattern>
</Servlet-mapping>
<Url-pattern>/servlet/TestServlet </url-pattern> is the access address of your form.
Then, tomcat finds and
<Servlet-mapping> the corresponding <servlet> configuration is as follows:
<Servlet>
<Servlet-name> TestServlet </servlet-name>
<Servlet-class> com. majianjie. TestServlet </servlet-class>
</Servlet>
Then, locate the address of the TestServlet. java File Based on <servlet-class>.
Then, you can access it in the browser by setting tomcat. For example, after I configure tomcat, enter http: // localhost: 8888/test_servlet/in the browser to access it,
Result
Click query:
Click the hyperlink B. jsp and the following is: