Java servlet Practice Test

Source: Internet
Author: User

Steps:

0. First create Web project, project name: Test_servlet

1, write Servlet,testservlet.java file content:

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. Add the servlet to Web. XML and add the contents of the file

<?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, write jump page 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" >

  

Test

Visit: Http://localhost/test_servlet/testServlet

Java servlet Practice Test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.