博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3.Servlet实例
阅读量:4981 次
发布时间:2019-06-12

本文共 4037 字,大约阅读时间需要 13 分钟。

一.基础实例

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); }}
View Code

 

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.界面效果

 

 

转载于:https://www.cnblogs.com/lukelook/p/9258044.html

你可能感兴趣的文章
redis常用命令与使用分析
查看>>
解决CSDN需要登录才能看全文
查看>>
linux下对于ntfs分区的访问
查看>>
比较Perl、PHP、Python、Java和Ruby
查看>>
.Net程序员学用Oracle系列(1):导航目录
查看>>
获取地址栏参数
查看>>
iOS开发JOSNModel<optional>,<convertondemand>,<index>
查看>>
SQL常用
查看>>
4.6上午
查看>>
[MacOS] 终端使用ssh时,中文乱码问题处理
查看>>
向大型网站学习SEO优化之道
查看>>
JQuery中ajax的相关方法总结
查看>>
良好的实践
查看>>
CentOS6.8 4.4.43内核 安装PF_RING
查看>>
typescript知识教程
查看>>
C++ 文件保存
查看>>
【转】狗日的开源软件许可证
查看>>
序列元素互异性算法
查看>>
POJ1251 || ZOJ1406 kruskal求最小生成树
查看>>
Struts2 02--通配符
查看>>