当前位置: 首页 > 编程日记 > 正文

第二章 Servlet核心技术 实训二

question.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>简单测试</title>
</head>
<body>
<p>请回答下面的问题:</p>
<form action="simpletest.do"method="post">
<p>1.Windows操作系统是哪个公司的产品?<input type="radio"name="q1"value="1">Apple公司<input type="radio"name="q1"value="2">IBM公司<input type="radio"name="q1"value="3">Microsoft公司<br>
<p>2.编写Servlet程序应继承哪个类?<input type="text"name="q2"size="30"><br>
<p>3.下面的程序设计语言,哪些是面向对象的?<input type="checkbox"name="q3"value="1">Java语言<input type="checkbox"name="q3"value="2">C语言<input type="checkbox"name="q3"value="3">C++语言<br>
<p>4.下图是哪种编程语言的徽标?<input type="radio"name="q4"value="1">C++<input type="radio"name="q4"value="2">Python<input type="radio"name="q4"value="3">Java<br><img src="logo.png.jpg"width="100"/><br>
<p>交卷请单击:<input type="submit"value="交卷">重答请单击:<input type="reset"value="重答">
</form></body>
</html>

ExamServlet.java

package com.demo;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 ExamServlet*/
@WebServlet(name = "examservlet", urlPatterns = { "/simpletest.do" })
public class ExamServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** Default constructor. */public ExamServlet() {// 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 stubresponse.getWriter().append("Served at: ").append(request.getContextPath());}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();String quest1 = request.getParameter("q1");//获得表单提交的参数String quest2 = request.getParameter("q2").trim();String[] quest3 = request.getParameterValues("q3");//获得表单提交的所有参数String quest4 = request.getParameter("q4");int score = 0;if(quest1!=null && quest1.equals("3")) {score = score+25;//答对一题加25分}if(quest2!=null && quest2.equals("HttpServlet")||quest2.equals("javax.servlet.http.HttpServlet")) {score = score+25;//答对一题加25分}if(quest3!=null && quest3.length==2 && quest3[0].equals("1")&&quest3[1].equals("3")) {score = score+25;//答对一题加25分}if(quest4!=null && quest4.equals("3")) {score = score+25;//答对一题加25分}out.println("<html><head>");out.println("<title>测试结果</title>");out.println("</head><body>");out.println("你的成绩是:"+score+"分");out.println("</body></html>");// TODO Auto-generated method stub//doGet(request, response);}}

input.jsp

<%@ 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>登录页面</title>
</head>
<body>
<form action = "first-servlet"method="post">学号<input type="text"name="sno"size="15"/><br>姓名<input type="text"name="sname"size="15"/><br><input type="submit"value="登录"/><input type="reset"value="取消"/>
</form></body>
</html>

Student.java

package com.demo;public class Student {private String sno;private String name;public Student(String sno,String name) {this.sno = sno;this.name = name;}public String getSno() {return sno;}public void setSno(String sno) {this.sno = sno;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

FirstServlet.java

package com.demo;import java.io.IOException;import javax.servlet.RequestDispatcher;
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 FirstServlet*/
@WebServlet("/first-servlet")
public class FirstServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public FirstServlet() {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 stubresponse.getWriter().append("Served at: ").append(request.getContextPath());}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String sno = request.getParameter("sno");String name = request.getParameter("sname");Student student = new Student(sno,name);request.setAttribute("student", student);RequestDispatcher rd = request.getRequestDispatcher("/second-servlet");rd.forward(request,response);// TODO Auto-generated method stubdoGet(request, response);}}

SecondServlet.java

package com.demo;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 SecondServlet*/
@WebServlet(name = "secondservlet", urlPatterns = { "/second-servlet" })
public class SecondServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public SecondServlet() {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 stubresponse.getWriter().append("Served at: ").append(request.getContextPath());}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Student student = (Student)request.getAttribute("student");response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();out.println("学号:"+student.getSno()+"<br>");out.println("姓名:"+new String(student.getName().getBytes("iso-8859-1"),"UTF-8")+"<br>");out.println("<a href='input.jsp'>返回输入页面</a>");// TODO Auto-generated method stubdoGet(request, response);}}

相关文章:

BZOJ 4025 二分图

题目大意 给定一个\(n\)个点, \(m\)条边的无向图, 每条边在一定时间范围内存在. 要你判断每个时间点这张图是否为二分图.\(n \le 10^5\)\(m \le 2 \times 10^5\) Solution 我们考虑一个合法的二分图有什么性质: 图中不存在奇环, 即环上边数(点数)为奇数的环. 考虑如何判断每个时…

javascript对象之window对象详解

frames 表示当前窗口中所有frame对象的数组 status 表示浏览器的状态行信息 defaultstatus 表示浏览器的状态行信息 history 表示当前窗口的历史记录,这可以引用在网页导航中 closed 表示当前窗口是否关闭的逻辑值 document 表示当前窗口中显示的当前文档对象 location 表示当前…

Wsus简单笔记

一&#xff1a;安装前的要求1&#xff1a;iis6.0以上&#xff0c;bits、Asp.net2.02:sql20053:Microsoft Management Console 3.04:microsof report viewer redistributable 20055&#xff1a;ntsf分区二&#xff1a;安装1&#xff1a;过程比较简单&#xff0c;注意设置本地补丁…

机器学习-Sklearn

Scikit learn 也简称 sklearn, 是机器学习领域当中最知名的 python 模块之一. Sklearn 包含了很多种机器学习的方式:Classification 分类 Regression 回归 Clustering 非监督分类 Dimensionality reduction 数据降维 Model Selection 模型选择 Preprocessing 数据预处理 我们总…

[翻译]自动维护索引重新生成组织的SQL批处理语句

脚本来自《Inside Server 2005 T-SQL Programming》 SET NOCOUNT ON;DECLARE objectid int;DECLARE indexid int;DECLARE partitioncount bigint;DECLARE schemaname nvarchar(258);DECLARE objectname nvarchar(258);DECLARE indexname nvarchar(258);DECLARE partitionnum bi…

DTrace memory leak 内存泄露

http://blog.sina.com.cn/s/blog_538040b70100eecn.html如下程序用于跟踪&#xff0c;在分配和回收都会触发探针 #!/usr/sbin/dtrace -s pid$target:libc:malloc:entry{ self->trace 1; self->size arg0;}pid$target:libc:malloc:return/self->trace 1/{ …

Spark的安装和使用

Spark2.1.0入门&#xff1a;Spark的安装和使用 Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0(2.7.1)/Ubuntu14.04(16.04) 手把手教你在VirtualBox中与主机共享文件夹

SQL Server 2005系列教学(6) 多表操作及子查询

多表查询&#xff1b;<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />人事表&#xff1a; 公司表&#xff1a;姓名性别年龄姓名公司地址张三男25李四女25张三新…

(点)分治学习笔记

哗我看了一下好像没有很详细专门讲分治的blog&#xff1f;那就主要先学一下点分治吧&#xff0c;其他的……等我记得把C一本通带到机房来再说吧先咕着啦 写在前面 刷题进度 入门题&#xff08;0/3&#xff09; 好题(0/9) 问题解决进度 Q1 Q2 正文 淀粉质 点分治 点分治就是在一…

十五个步骤收获学习的习惯

"真正的发现的航程&#xff0c;并非是在寻找新的土地,而且用新的视界去寻找"--普鲁斯特 "智慧日进者方值得尊敬。"-林肯 "我从不让我在学校所学的干扰我的教育"-马克吐温 如果公立学校尚未摧残你的灵魂&#xff0c;那么学习是一项极佳的活动。它…

熟悉scala命令,scala语言运行超级素数和猴子大王

实验目的 在Linux操作系统中安装Scala输入“scala”命令&#xff0c;熟悉地运行Scala解释器scala语言运行超级素数和猴子大王实验仪器 Virtualbox管理器 实验框图&#xff08;电路图/流程图&#xff09; 在Windows中使用VirtualBox安装Ubuntu&#xff0c;安装好scala后&#xf…

安装mayavi和VTK库的血泪史

一开始安装VTK库是从官网上下载&#xff0c;但是怎么都找不到whl文件&#xff0c;只有exe文件&#xff08;vtkpython-7.1.1-Windows-64bit.exe&#xff09;。下载安装之后再PyCharm中import vtk出错。当时认为是文件出错。后来在一篇博客&#xff08;Python下VTK 编程 - lj6952…

Python LEGB (Local, Enclosing, Global, Build in) 规则

1 Local 一个函数定义了一个 local 作用域; PyFrameObject 中的 f_local 属性2 Global 一个 module 定义了一个 global 作用域; PyFrameObject 中的 f_global 属性.3 BuiltIn open, dir 的作用域等等, python 最顶层的作用…

图解DotNet框架系列

图解.Net框架系列(索引贴) (声明:本系列已完成,故索引帖重发) 众所周知,DotNet框架是非常庞大的,光项目创建时的种类就有WPF,WCF,WF这三种最新的技术,还有以前的Web,WinForm,Service,Mobile等等. 这么复杂和庞大的框架,用文字来描述是远远不够的,所以我准备写一系列图文并茂的文…

【Linux基础】文件处理实例

1.文件拆分 //每4000行拆分一个文件 split -l 4000 epms_t_ep_fx_stl_xy_20190129.dat 2.行处理 //查找第二列为711611且第三列为711100记录&#xff0c;打印行号和整行数据 awk -F ‘^C’ {if ($3711100 && $2711611) print NR,$0 } epms_t_ep_fx_stl_xy_20190229.d…

scala语言运行递归“分鱼”程序

A、B、C、D、E 五人在某天夜里合伙去捕鱼&#xff0c;到第二天凌晨时都疲惫不堪&#xff0c;于是各自找地方睡觉。 日上三杆&#xff0c;A 第一个醒来&#xff0c;他将鱼分为五份&#xff0c;把多余的一条鱼扔掉&#xff0c;拿走自己的一份。 B 第二个醒来&#xff0c;也将鱼分…

smarty_modifier_truncate,无或者有md_substr的情况下都能正确截取字符串的php函数,可用于smarty。...

smarty_modifier_truncate,无或者有md_substr的情况下都能正确截取字符串的php函数&#xff0c;可用于smarty。function smarty_modifier_truncate($string, $length 80, $etc ..., $codeutf8, $mbtrue) { if ($length 0) return $string; if(function_exists("mb_subs…

java基础小总结(2)

Day07&#xff1a; 1.如果局部变量和全局变量都有相同的数据类型和变量名&#xff0c;先调用局部变量&#xff0c;实现就近原则&#xff1b; 2.匿名对象由于局限性的原因&#xff0c;一般只调用一次&#xff0c;且只是当作为实际参数传递的时候使用&#xff1b; 3.面向对象语言…

Wireshark实验HTTP

在 Wireshark 实验入门里&#xff0c;我们已经初步使用了 Wireshark 包嗅探器&#xff0c;我们现在可以操作 Wireshark 来查看网络协议。在这个实验中&#xff0c;我们会探索 HTTP 协议的几个方面&#xff1a;基本的 GET/response 交互&#xff0c;HTTP 消息格式&#xff0c;检…

2、安装ICS(Internet Component Suite)控件

下载完成后解压到你的指写目录&#xff01;1、在library里加入ICS->Delphi->Vc32目录。2、从File->Open中打开ICS->Delphi->Vc32->IcsDel110.dproj文件。(文件名在其它Delphi版本略有不同)3、在项目管理器中&#xff0c;右键IcsDel110.bpl选择Build和Install…

定制CE系统随笔-续1

更改用户界面颜色[HKEY_LOCAL_MACHINE\SYSTEM\GWE] "SysColor"hex: 00,00,00,00, 3A,6E,A5,00, 00,00,00,00, 00,00,00,00,\ EF,EB,DE,00, FF,FF,FF,00, 00,00,00,00, 00,00,00,00,\ 00,00,00,00, FF,F…

安装包安全测试

主要说明以下内容&#xff1a;1、能否反编译代码2、安装包是否签名3、完整性校验4、权限设置检查反编译代码&#xff1a;移动应用发布出去后最终用户获得的是一个程序安装包&#xff0c;我们需要关注的是用户能否从这个安装包中获取项目的源代码&#xff0c;从安全方面考虑&…

Java课程寒假之开发记账本软件(网页版)之二

一.实现基础功能之一&#xff08;记账&#xff09; 一个记账本最基础之一的功能就是记账&#xff0c;所以也是首先要解决的问题&#xff0c;我选择了上学期使用的MySQL数据库来对账本进行存储。 我选择记账的方法是分开记账&#xff0c;就是支出放在一个表&#xff0c;收入放在…

谷歌浏览器Google Chrome和Adobe Flash Plugins插件安装问题

最近在做CSS的多浏览器支持&#xff0c;于是安装上了谷歌浏览器Google Chrome浏览器&#xff0c;结果发现谷歌浏览器Google Chrome的确构造非常简单&#xff0c;精干&#xff0c;速度非常迅猛&#xff0c;比臃肿的IE8快多了&#xff0c;于是开始使用谷歌浏览器Google Chrome&am…

Wireshark实验 - 入门

# Wireshark实验 - 入门 **官方英文文档&#xff1a;[Wireshark_Intro_v6.0.pdf](Wireshark_Intro_v6.0.pdf)** **以下内容为笔者翻译&#xff1a;** *** ## Wireshark 实验: 入门 v6.0 **《计算机网络&#xff1a;自顶向下方法&#xff08;第6版&#xff09;》补充材料&…

观察者模式的经典应用(猫叫 烧开水)

Code 猫叫了 老鼠跑 主人惊醒 1/**//* 2 * 题目&#xff1a; 3 * 猫叫了&#xff0c;所有老鼠开始逃跑&#xff0c;主人被惊醒&#xff0c;请用OO的思想描绘此过程 4 * 1&#xff0c;老鼠跟主人是被动的 5 * 2&#xff0c;要考虑联动性与扩展性 6 */ 7using System; 8using Sys…

HTML学习笔记之基本介绍

超文本标记语言 (Hyper Text Markup Language&#xff0c;HTML&#xff09;不是一种编程语言&#xff0c;而是一种标记语言&#xff0c;用一套标记标签描述网页 HTML 标记标签又被称为 HTML 标签&#xff08;HTML Tag&#xff09;&#xff0c;它是由尖括号包围的关键词&#xf…

系统分析与设计 实验一用例模型

图书管理系统系统分析及用例图 图书管理系统能够为一定数量的借阅者提供服务。每个借阅者能够拥有唯一标识其存在的编号。图书馆向每一个借阅者发放图书证&#xff0c;图书证中包含每一个借阅者的编号和个人信息。系统通过一个单独的程序为借阅者提供服务&#xff0c;不需要管理…

2017.9.12.语文

列夫尼古拉耶维奇托尔斯泰&#xff08;Лев Николаевич Толстой&#xff0c;1828年9月9日&#xff0d;1910年11月20日&#xff09;19世纪中期俄国批判现实主义作家、思想家、哲学家。 转载于:https://www.cnblogs.com/mldyz/p/7510750.html

一封会笑死人的校园情书

Kiss郝美丽&#xff1a; Sorry&#xff01;我把Miss拼成了Kiss&#xff0c;一不小心吻了你&#xff0c;实在是对不起&#xff01; 吾本良家子弟&#xff0c;正统少年&#xff0c;一向对美眉们保持一种昂首挺胸&#xff0c;目不斜视的高姿态&#xff0c;人送美名…