1.导入相关的包:commons-lang.jar、commons-logging-1.0.4.jar、junit.jar、log4j-1.2.8.jar、ojdbc14.jar、servlet-api.jar
2.配置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">
<display-name></display-name>
<servlet>
<servlet-name>studentservlet</servlet-name>
<servlet-class>com.ck.studentmanage.action.StudentManageAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentservlet</servlet-name>
<url-pattern>/StudentManageAction</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
servlet-class 填写servlet 的全路径,servlet映射的名字要与上面的一样,url-pattern 为jsp访问路径,可以随意填写。
3. 编写servlet类:
new一个servlet类(也可以自己写),new一个的话,有默认的配置,不需要手动写
之后next取个名字,再一直next 就行了
一下是我测试的servlet:
package com.ck.studentmanage.action;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ck.studentmanage.I_student;
import com.ck.studentmanage.impl.StudentManageImpl;
import com.ck.studentmanage.impl.StudentVo;
public class StudentManageAction extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String flag = request.getParameter("flag");
StudentVo vo = null;
List<StudentVo> list = new ArrayList<StudentVo>();
I_student obj = new StudentManageImpl();
if("queryList".equals(flag)){
try {
vo = new StudentVo();
vo.setId("1");
list = obj.queryList(vo);
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("list", list);
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4.下面说一下关于jsp页面的取值,以下是我测试的页面:
通过链接:http://localhost:8088/running_servlet/s?flag=queryList;
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.ck.studentmanage.impl.*" %>
<%
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>
<table>
<tr><td>name</td><td>sex</td></tr>
<%
List list = (List)request.getAttribute("list");
StudentVo vo = new StudentVo();
if(null!=list){
for(int i=0;i<list.size();i++){
vo = (StudentVo)list.get(i);
%>
<tr><td><%=vo.getName() %></td><td><%=vo.getSex() %></td></tr>
<%}
}%>
</table>
</body>
</html>
这里需要注意的是:要想使用List集合、StudentVo对象,必须在jsp页面的最上面加上<%@ page import="com.ck.studentmanage.impl.*" %>导入集合对象所在的包</span>