MyFirstServlet学习笔记

MyFirstServlet学习笔记

项目文件的结构目录如下,ser包,下设Serv.java是一个servlet文件,Note只是做的笔记,webroot下有login.jsp,WEB-INF下有web.xml,lib当中有servlet-api.jar文件。

首先是login.jsp文件,Form表单的action指向Servlet名称,在本项目中为Serv,method方法post或者get都可以。

下面是Serv.java文件,位于ser包下,使用request.getParmeter("")获取jsp页面form表单中值,在out.println中输出一个HTML页面,使用doPost方法引用doGet方法,这样不管form表单使用method哪个方法,servlet都可以进行处理。

接下来是web.xml的配置问题,所有的servlet配置信息都要写在web-app标签中,标签<servlet>当中,name即为servlet类名,servlet-class内容写包名.类名,下面的servlet-mapping标签中的name和上面的servlet名称相同,url-pattern标签的内容需要和jsp页面form表单中action的内容必须相同,注意url-pattern内容中有一个斜杠/,而action当中没有斜杠。

最后说明一下jsp+servlet+web.xml的运行过程:

运行jsp之后,form表单在浏览器页面当中显示出来,点击按钮submit之后,form表单首先指向action当中的内容,接着去web.xml里面的url-pattern当中查找和action内容相同的内容,所以action内容和url-pattern内容必须一致,(内容可以随便写,但必须内容一样,否则会出现页面找不到url-pattern的错误)。相同的url-pattern内容找到之后,然后程序根据当前url-pattern标签所在的servlet-mapping找servlet-name(所以这两个name需要相同),再根据这个name找当前<servlet>标签中那么相同的,然后根据后面的这个name找class文件,这样就找到了Serv.class文件,就可以运行Serv.java文件了。

具体的图片说明:action指向1的url-pattern,然后1指向2的name,2去找servlet标签中相同的name3,3去找class对应的路径文件,就是这样一个调用关系。

最后附上项目三个主要文件源代码:

index.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>登陆</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24     <!-- action里面的内容需要和web.xml中url-pattern中内容相同,可以随便写,但必须相同,
25     因为submit之后指向action当中的内容,这时程序会找web.xml当中的url-pattern -->
26       <form id="form1" name="form1" action="Serv" method="post">
27           用户名:<input type="text" id="username" name="username" > <br>
28           密码:<input type="password" id="password" name="password" > <br>
29           <input type="submit" value="登陆">
30       </form>
31   </body>
32 </html>

Serv.java

 1 package ser;
 2 //导入必需的 java 库
 3 import java.io.*;
 4 import javax.servlet.*;
 5 import javax.servlet.http.*;
 6 //扩展 HttpServlet 类
 7 public class Serv extends HttpServlet {
 8     // 处理 GET 方法请求的方法
 9     public void doGet(HttpServletRequest request, HttpServletResponse response)
10             throws ServletException, IOException {
11         // 设置响应内容类型
12         response.setContentType("text/html;charset=utf-8");
13         
14         PrintWriter out = response.getWriter();
15         String title = "Using POST Method to Read Form Data";
16         String docType = "<!doctype html public "-//w3c//dtd html 4.0 "
17                 + "transitional//en">
";
18         out.println(docType + "<html>
" + "<head><title>" + title
19                 + "</title></head>
" + "<body bgcolor="#f0f0f0">
"
20                 + "<h1 align="center">" + title + "</h1>
" + "<ul>
"
21                 + "  <li><b>用户名</b>:" + request.getParameter("username") + "
"
22                 + "  <li><b>密码</b>:" + request.getParameter("password") + "
"
23                 + "</ul>
" + "</body></html>");
24     }
25     // 处理 POST 方法请求的方法
26     public void doPost(HttpServletRequest request, HttpServletResponse response)
27             throws ServletException, IOException {
28         doGet(request, response);
29     }
30 }

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5     id="WebApp_ID" version="3.0">
 6     
 7     <welcome-file-list>
 8         <welcome-file>login.jsp</welcome-file>
 9     </welcome-file-list>
10     
11     <servlet>
12         <!--servlet类名 -->
13         <servlet-name>Serv</servlet-name>
14         <!--servlet所在的包名.servlet类名 -->
15         <servlet-class>ser.Serv</servlet-class>
16     </servlet>
17 
18     <servlet-mapping>
19         <!--servlet类名,和上面的类名必须相同 -->
20         <servlet-name>Serv</servlet-name>
21         <!-- jsp页面的form表单中action的内容,和这里相同 -->
22         <url-pattern>/Serv</url-pattern>
23     </servlet-mapping>
24 
25 </web-app>

以上内容只是本人根据个人经历得出,如有不对或者不妥的地方,还请不吝赐教。

原文地址:https://www.cnblogs.com/diyunfei/p/6549843.html