標籤:instance 技術分享 port tchar webapp 步驟 info dispatch over
步驟:
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 {@Overrideprotected 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);}@Overrideprotected 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練習測試