java servlet練習測試

來源:互聯網
上載者:User

標籤: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練習測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.