J2ee之jsp+Servlet配置

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>


大致的配置就这些,方法的实现就请自己完成。

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值