为什么想写这个?
突然兴起,想研究研究servelt
servlet是什么?
servlet=server+applet(小服务),全程 java servlet,是用java语言编写的服务端程序.主要功能就是为前端的请求提供服务(官方描述是:交互式地浏览和修改数据,生成动态web内容),
狭义的servlet是java语言实现的一个接口,广义的servlet是任何实现这个servlet接口的类.
servlet出现背景?
简单来说是为了完成客户端和服务端的数据交互.
事情的经过其实是这样的,一开始,实现数据交互是通过编写CGI(通用网关接口),但是CGI方式的运行要开启多个进程,速度慢,效率低,相当消耗系统资源,因此出现了用多线程代替多进程的方式来解决这个数据交互问题,这就是servlet
servlet demo
运用servet实现增删改查,
index.jsp:操作页面.
ok.jsp:成功页面
UserServlet:处理类
web.xml:配置servlet的映射
代码:
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="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>
servlet 增 删 改 查 <br>
<form action="userServlet" method="post">
<input type="submit" value="增"></input>
<input type="hidden" type="method" name="method" value="add">
</form><br>
<form action="userServlet" method="post">
<input type="submit" value="删"></input>
<input type="hidden" name="method" value="del">
</form><br>
<form action="userServlet" method="post">
<input type="submit" value="改"></input>
<input type="hidden" name="method" value="modify">
</form><br>
<form action="userServlet" method="post">
<input type="submit" value="查"></input>
<input type="hidden" name="method" value="select">
</form><br>
</body>
</html>
ok.jsp
<%@ page language="java" import="java.util.*" pageEncoding="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 'ok.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>
ok <br>
</body>
</html>
UserServlet.java
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class UserServlet implements 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 config) throws ServletException {
// TODO Auto-generated method stub
}
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
// TODO Auto-generated method stub
String method=(String)req.getParameter("method");
RequestDispatcher rd=req.getRequestDispatcher("ok.jsp");
if("add".equals(method)){
System.out.println("增");
rd.forward(req, res);
}
if("del".equals(method)){
System.out.println("删");
rd.forward(req, res);
}
if("modify".equals(method)){
System.out.println("改");
rd.forward(req, res);
}
if("select".equals(method)){
System.out.println("查");
rd.forward(req, res);
}
}
}
web.xml
<?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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>userServlet</servlet-name>
<servlet-class>UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>userServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
</web-app>