Servlet实质上就是按Servlet规范编写的Java类。
Servlet 使用Java语言编写,不仅继承了Java语言中的优点,而且还对Web的相关应用进行了封装。
Servlet 程序一次编码,多平台运行,拥有超强的移植性。
Servlet 和JSP的区别:
1、JSP页面可以存在HTML代码与Java代码并存的情况,Servlet 只有调用固定的方法才能将动态内容输出为静态的HTML。
2、Servlet代码中,需要调用Servlet提供的相关API接口方法,才可以对HTTP请求及业务进行处理,对于业务逻辑方面的处理功能更加强大。JSP页面中,通过HTML代码与JSP内置对象实现对HTTP请求及页面的处理,其显示界面的功能更加强大。
3、Servlet需要在Java编译器编译后才可以运行,JSP文件无论被创建还是修改都不需要对其编译即可执行。
4、JSP的执行效率低于Servlet的执行效率。
5、Servlet代码需要在web.xml配置才能实现,(下面的前两个代码还未在web.xml中配置)
----------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
//初始化方法
public void init() throws ServletException {
}
//处理HTTP Get请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
//处理HTTP Post请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
//处理HTTP Put请求
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
//处理HTTP Delete请求
public void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
//销毁方法
public void destroy() {
super.destroy();
}
}
这是Servlet代码的基本结构。在Java中,Servlet是指HttpServlet对象,在声明一个对象为Servlet时,需要继承HttpServlet类。上述代码包含有6中方法,其中init()方法与destroy()方法为Servlet初始化与生命周期结束所调用的方法,其余的4个方法为Servlet针对处理不同的HTTP请求类型所提供的方法,作用如注解所示。
----------------------------------------------------------------------------------------------------------------------------------------------------------Servlet是运行在服务器端的Java应用程序,由Servlet容器对其进行管理,当用户对容器发送HTTP请求时,容器将通知相应的Servlet对象进行处理,完成用户与程序之间的交互。
public void destroy() {
// TODO Auto-generated method stub
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
PrintWriter pwt = response.getWriter();
pwt.println("mingrisoft");
pwt.close();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
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 MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>Servlet实例</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" Servlet实例: ");
out.print(this.getClass());
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.lyq.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/servlet/MyServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>servlet/MyServlet</welcome-file>
</welcome-file-list>
</web-app>