java servlet练习测试

Posted 风清云流

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java servlet练习测试相关的知识,希望对你有一定的参考价值。

步骤:

0、首先创建web project,工程名:test_servlet

1、编写Servlet,TestServlet.java文件内容:

package com.ouyang.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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;

@SuppressWarnings("serial")
public class TestServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");
		
		String url = "jdbc:mysql://localhost:3306/test01";
		String user = "root";
		String password = "xxx";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		String sql = "select id, name, age from individual_information";
		List<List<String>> record = new ArrayList<>();

		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(url, user, password);
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();

			List<String> tmp = new ArrayList<>();
			while (resultSet.next()) {
				tmp.add(resultSet.getString("id"));
				tmp.add(resultSet.getString("name"));
				tmp.add(resultSet.getString("age"));
				record.add(new ArrayList<String>(tmp));
				tmp.clear();
			}

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		req.setAttribute("individual_information", record);
		RequestDispatcher requestDispatcher = req
				.getRequestDispatcher("get_info.jsp");
		requestDispatcher.forward(req, resp);

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
}

  

2、将servlet添加到web.xml, 添加后的文件内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>test_web</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>testServlet</servlet-name>
  	<servlet-class>com.ouyang.servlet.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>testServlet</servlet-name>
  	<url-pattern>/testServlet</url-pattern>
  </servlet-mapping>  
  
</web-app>

  

3、编写跳转页面 get_info.jsp

<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>get_info</title>
</head>
<body>
	<%
		@SuppressWarnings("unchecked")
		List<List<String>> record = (List<List<String>>)request.getAttribute("individual_information");
		for(List<String> list : record){
			out.println("<li>" + list.toString() + "</li>");
		}
	
	%>
</body>
</html>

  

【测试】

访问:http://localhost/test_servlet/testServlet

 

以上是关于java servlet练习测试的主要内容,如果未能解决你的问题,请参考以下文章

Servlet练习

java---servlet与filter的联系与区别

Java全栈web网页技术:11.书城项目实战二:JSP练习及Servlet优化

Java全栈web网页技术:11.书城项目实战二:JSP练习及Servlet优化

Java全栈web网页技术:11.书城项目实战二:JSP练习及Servlet优化

jsp初识servlet转发转发与重定向的比较