java servlet练习测试
生活随笔
收集整理的這篇文章主要介紹了
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 {@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
?
轉載于:https://www.cnblogs.com/ouyxy/p/7216804.html
總結
以上是生活随笔為你收集整理的java servlet练习测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java enum分析
- 下一篇: 安装PyQt5安装包或者其他操作,报错F