这个项目是我在慕课网上看到的,《JAVA遇见HTML--Servlet篇》,项目并不是我原创,但因为要写的是我的学习笔记,所以标了原创。
一、什么是Servlet?
servlet是服务器上运行的小程序。一个Servlet就是一个JAVA类,并且可以通过“请求-响应”编程模型来访问这个驻留在服务器内存里的Servlet程序。
二、Tomact容器等级
Tomact的容器分为四个等级,Servlet的容器管理Context容器,一个Context对应一个Web工程。
三、Servlet基础
1、继承HttpServlet
2、重写doget()或dopost()方法
3、在web.xml中注册Servlet
四、一个小程序
上面是效果图,分别点击超链接和按钮都会出来 Hello Servlet!
下面是代码
1、index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>使用MyEclipse创建Servlet小例子</h1>
<hr>
<a href="servlet/HelloServlet">Get方式请求HelloServlet</a><br>
<form action = "servlet/HelloServlet" method = "post">
<input type = "submit" value = "Post方式请求HelloServlet"></input>
</form>
</body>
</html>
2、然后编写Java类 包名类型分别对应JSP中的servlet/HelloServlet
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("处理get请求。。。");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<strong>Hello Servlet!</strong>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("处理post请求。。。");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<strong>Hello Servlet!</strong>");
}
}
这里有很多注意点
(1)生成类的时候 要继承HttpServlet 这个类 我一开搜索的时候没有这个类,原因是没有导入JAR包
导入Jar包参考:http://blog.sina.com.cn/s/blog_9cbb6a210102v0f6.html
(2)重写的doGet 和 doPost 方法是直接插入的 然后Ecplise里会有一个super方法,要去掉,不然不执行。
3、在xml里注册Servlet
<?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">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/servlet/HelloServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
注册时候注意对应<Servlet> 和<Servlet-mapping>里的类就好
我看那个慕课视频下面好多人问为什么要在xml里注册Servlet ,我表示我也不是特别清楚,但是项目都这么做。。。反正先做起来再说