servlet MVC
下面给出了一个非常简单的模板:
第一步:准备索引页面和转向页面index.jsp,a.jsp
第二步:准备配置文件web.xml
第三步:准备servlet.jar(Tomcat中common目录有)和servlet测试类
1 <%@ page language="java" contentType="text/html; charset=GB18030"
2 pageEncoding="GB18030"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <h1>测试Servlet</h1>
11 <hr>
12 <!-- 页面里面的路径都是相对路径前面不需要斜杠,如action="servlet/TestServlet"和href="a.jsp",
13 默认情况下servlet都会将类放在/servlet下面,所以配置的时候要加上,同时需要和web.xml配置文件中
14 保持一致。 -->
15 <form action="servlet/TestServlet" method="post">
16 <input type="text" name="username"><br>
17 <input type="submit" value="提交"><br>
18 </form>
19 <a href="a.jsp">a.jsp</a><br>
20 </body>
21 </html>
2 pageEncoding="GB18030"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <h1>测试Servlet</h1>
11 <hr>
12 <!-- 页面里面的路径都是相对路径前面不需要斜杠,如action="servlet/TestServlet"和href="a.jsp",
13 默认情况下servlet都会将类放在/servlet下面,所以配置的时候要加上,同时需要和web.xml配置文件中
14 保持一致。 -->
15 <form action="servlet/TestServlet" method="post">
16 <input type="text" name="username"><br>
17 <input type="submit" value="提交"><br>
18 </form>
19 <a href="a.jsp">a.jsp</a><br>
20 </body>
21 </html>
1 <%@ page language="java" contentType="text/html; charset=GB18030"
2 pageEncoding="GB18030"%>
3 <%@ page import="java.util.*" %>
4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5 <html>
6 <head>
7 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
8 <title>Insert title here</title>
9 </head>
10 <body>
11 c.jsp
12 <p>
13 I am here!<br>
14 <%
15 String name = "inital";
16 name = (String)request.getAttribute("username");
17 out.println(name);
18 %>
19 </body>
20 </html>
2 pageEncoding="GB18030"%>
3 <%@ page import="java.util.*" %>
4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5 <html>
6 <head>
7 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
8 <title>Insert title here</title>
9 </head>
10 <body>
11 c.jsp
12 <p>
13 I am here!<br>
14 <%
15 String name = "inital";
16 name = (String)request.getAttribute("username");
17 out.println(name);
18 %>
19 </body>
20 </html>
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.4"
3 xmlns="http://java.sun.com/xml/ns/j2ee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
6 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
7 <welcome-file-list>
8 <welcome-file>index.jsp</welcome-file>
9 </welcome-file-list>
10
11 <servlet>
12 <servlet-name>TestServlet</servlet-name>
13 <servlet-class>com.klein.servlet.TestServlet</servlet-class>
14 </servlet>
15
16 <servlet-mapping>
17 <servlet-name>TestServlet</servlet-name>
18 <url-pattern>/servlet/TestServlet</url-pattern>
19 </servlet-mapping>
20 </web-app>
2 <web-app version="2.4"
3 xmlns="http://java.sun.com/xml/ns/j2ee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
6 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
7 <welcome-file-list>
8 <welcome-file>index.jsp</welcome-file>
9 </welcome-file-list>
10
11 <servlet>
12 <servlet-name>TestServlet</servlet-name>
13 <servlet-class>com.klein.servlet.TestServlet</servlet-class>
14 </servlet>
15
16 <servlet-mapping>
17 <servlet-name>TestServlet</servlet-name>
18 <url-pattern>/servlet/TestServlet</url-pattern>
19 </servlet-mapping>
20 </web-app>
1 package com.klein.servlet;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 public class TestServlet extends HttpServlet {
11
12
13 @Override
14 protected void service(HttpServletRequest request, HttpServletResponse response)
15 throws ServletException, IOException {
16 String name = request.getParameter("username");
17
18 /*
19 request.getRequestDispatcher("另一个Servlet的请求url").forward(request, response);
20 response.sendRedirect("另一个Servlet的请求url");
21 */
22 request.setAttribute("username", name);
23 request.getRequestDispatcher("../a.jsp").forward(request, response);
24 }
25 }
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 public class TestServlet extends HttpServlet {
11
12
13 @Override
14 protected void service(HttpServletRequest request, HttpServletResponse response)
15 throws ServletException, IOException {
16 String name = request.getParameter("username");
17
18 /*
19 request.getRequestDispatcher("另一个Servlet的请求url").forward(request, response);
20 response.sendRedirect("另一个Servlet的请求url");
21 */
22 request.setAttribute("username", name);
23 request.getRequestDispatcher("../a.jsp").forward(request, response);
24 }
25 }