您的位置:首页 > 其它

Servlet简介

2017-04-02 19:15 288 查看
Servlet简介
     俗话说先有鸡还是先有蛋这个问题还真的是不知道

,但是先有JSP还是先有Servlet,这毫无疑问,先有Servlet,JSP的前身就是Servlet.
Servlet是在服务器中运行的一段小程序,一个Servlet就是一个JAVA类,并且可以通过请求—响应模型来访问这个留在服务器内存里的Servlet程序。



手工编写Servlet程序步骤:
1. HttpServlet是一个抽象类不能直接实例化,必须要继承于它。
2.重写doGet()或者doPost()方法。
如果是get请求就重写doGet()方法,如果是post请求就重写doPost()方法
3.在web.xml中注册Servlet
<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>
这个就是web.xml,<servlet-name>HelloServlet</servlet-name>这个指定类名,<url-pattern>/servlet/HelloServlet</url-pattern>指定路径,后面与jsp代码中超链接地址一样,这样才可以访问。

java类
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 {

/**
* Constructor of the object.
*/
public HelloServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

System.out.println("处理Get请求。。。。");
PrintWriter out=response.getWriter();
response.setContentType("text/html;charset=utf-8");
out.println("<strong>Hello Servlet!</strong><br>");
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("处理Post()请求。。。。");
PrintWriter out=response.getWriter();
response.setContentType("text/html;charset=utf-8");
out.println("<strong>Hello Servlet!</strong><br>");
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}

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 h
4000
ttp-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>第一个Servlet小例子</h1>
<hr>
<a href="servlet/HelloServlet">Get方式请求HelloServlet</a>
<form action="servlet/HelloServlet" method="post">
<input type="submit" value="Post方式请求HelloServlet">
</form>
</body>
</html>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd"> <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>

</web-app>

这个程序在类里面定义了俩种方法,post那里是个提交表单的代码,不同的方式调用不同的方法,get调用doGet方法,post调用doPost方法,观察后台显示



在页面里点那个,后台会相应打出调用哪个方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Servlet简介