一.基础实例
1.参照如下例子创建maven web工程:
https://www.cnblogs.com/lukelook/p/9187313.html
2.创建一个简单的Servlet 类
package com.my.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class CallWeb */public class CallWeb extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CallWeb() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("").append("This is just a test!").append("
"); response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
3.在web.xml中配置如下:
4.Run On Server CallWeb.class
5.在浏览器中输入如下地址即可访问:
http://localhost:8080/myWeb/CallWeb
二.表单数据提交
1.JSP界面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>show
2.Servlet处理类
package com.my.web;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class Show */@WebServlet("/CallWeb")public class CallWeb extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CallWeb() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("班级:"+request.getParameter("stuClass")+""); pw.println("用户名:"+request.getParameter("stuName")+""); pw.println("密码:"+request.getParameter("stuPassword")+""); pw.println("性别:"+request.getParameter("sex")+""); String[] arr=request.getParameterValues("love"); for (String string : arr) { pw.println("兴趣:"+string+""); } pw.println("地址:"+request.getParameter("address")+""); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
3.界面效果