首页 > 技术文章 > 小白之路 | 从小学一年级期末考试看servlet+jsp技术

tangdiao 2020-12-09 13:18 原文

先上界面

  (前端可以使用css、js等库进行美化,相关章节可以参见前面关于css、js的总结。当然,“技术不是最重要的,最重要的想法、创意瞎扯,都重要!其实艺术和技术的结合,是一件挺有意思的事情)

  1. 本地tomcat上部署后,访问链接:http://localhost:9001/BS/

成绩单:

  

源码:

index.jsp:

<%@page contentType="text/html;charset=GBK"%>
<html>
<head>
<title>小学一年级数学期末考试</title>
</head>
<style>
#all{
    width: 800px;
    height: 450px;
    border: 1px solid green;
    position:absolute;
    top:50%;
    left:50%;
    margin:-225px 0 0 -400px;
}
</style>

<%
        int errorTag=0;////errorTag!=0是因为用户非法输入,非法输入不更新题目
        if(session.getAttribute("errorTagInput")!=null){
            errorTag = (int)session.getAttribute("errorTagInput");
            session.setAttribute("errorTagInput", null);// 重新监听非法输入        
        }
        
        if(errorTag == 0){// 正常输入
            int a = (int)(Math.random() * 1000) + 1;
            int b = (int)(Math.random() * 1000) + 1; 
            session.setAttribute("a", a);
            session.setAttribute("b", b);
        }
         
%>
<!-- 显示正确/错误的提示信息 -->
<jsp:useBean id="ci" scope="session" class="exec.CInfo">
    <jsp:setProperty name="ci" property="info" value="****" />
</jsp:useBean>

<body>
<div id="all" style="border:1px #cccccc solid; width:50%; height:80%px;
text-align:center;">
<br>
<h1>小学一年级期末考试</h1>
<h2>计时开始...</h2>
<br>
<b>Please Answer:</b><br>
<%= session.getAttribute("a") %> + <%= session.getAttribute("b") %>=
<br><br>
<b>The last time to answer:</b>
<p style="color:red"><jsp:getProperty name="ci" property="info" /><p>
<br>
<!-- 输入猜数的表单界面 -->
<form method="POST" action="Action">
<b>Answer:<input type="text" name="guessNumber" size="20"></b>
<input type="submit" value="SUBMIT" name="B1">
</form>
<form method="POST" action="Action">
<input type="submit" value="QUIT" name="B1">
</form>
</div>
</body>
</html>

 

success.jsp:

<%@page contentType="text/html;charset=GBK"%>
<html>
<head>
<title>数学期末成绩</title>
</head>
<pre>
<%
        if(session.getAttribute("true_total")!=null && session.getAttribute("total")!=null){
            int true_total = (int)session.getAttribute("true_total");
            int total = (int)session.getAttribute("total");
            out.println("正确数:" + true_total);
            out.println("答题数:" + total);
            out.println("正确率:" + String.format("%.2f",true_total*100.0/total)+"%");          
        }else{
            out.println("请不要交白卷!");
        }
        session.invalidate();                 
%>
</pre>

<body>
<p><a href='index.jsp'>是否对成绩还不满意?点击重考!</a>
</body>
</html>

 

Action.java:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//判断题目是否正确的Servlet类,并做相应的处理
public class Action extends javax.servlet.http.HttpServlet implements
        javax.servlet.Servlet {

    // 构造函数
    public Action() {
        super();
    }

    // 记录题目数
    int total = 0;
    int true_total = 0;
    // 处理GET请求
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        // 设置发送到客户端的内容类型
        response.setContentType("text/html;charset=GB2312");
        
        //判断点击的是哪个按钮
        String btn = request.getParameter("B1");
        if(btn.equals("QUIT")) {//如果是退出按钮,直接返回了,不用执行下面else里面的代码了
            total = 0;
            true_total = 0;
            RequestDispatcher dispatcher = request
                    .getRequestDispatcher("success.jsp");
            dispatcher.forward(request, response);
        }else {
        
        // 获取客户端写入器
        PrintWriter out = response.getWriter();
        // 获取客户端请求信息中的guessNumber属性值
        String s1 = request.getParameter("guessNumber");
//        System.out.println(s1);
        // 获取当前会话
        HttpSession session = request.getSession(true);
        session.setAttribute("total", ++total);
        session.setAttribute("true_total", true_total);//为了防止全答错的情况
        
        // 获取会话信息中的ci属性
        exec.CInfo ci = (exec.CInfo) session.getAttribute("ci");
        int num1=0;    
        int a = (int) session.getAttribute("a");
        int b = (int) session.getAttribute("b");
        System.out.println(a+"+"+b);
                
        // 得到输入的内容
        if(isNumeric(s1)) {
            num1 = Integer.parseInt(s1);
        }else {
            session.setAttribute("errorTagInput", 1);
            ci.setInfo("Input error, please retype******\n"+a+"+"+b+"=");
            RequestDispatcher dispatcher = request
                    .getRequestDispatcher("index.jsp");
            dispatcher.forward(request, response);
        }
        
        // 如果猜数与系统生成的初始随机数相等
        if (num1 == a+b) {
            // 跳转到成功页面
            session.setAttribute("true_total", ++true_total);
            ci.setInfo(a+"+"+b+"="+num1+"******"+"True");
            RequestDispatcher dispatcher = request
                    .getRequestDispatcher("index.jsp");
            dispatcher.forward(request, response);
            // 结束当前会话
//            session.invalidate();
        }
        
        else {
            // 设置显示的提示信息
            ci.setInfo(a+"+"+b+"="+num1+"******"+"False"+"******The correct result:"+(a+b));
            // 跳转到主页面页面
            RequestDispatcher dispatcher = request
                    .getRequestDispatcher("index.jsp");
            dispatcher.forward(request, response);
            // 结束当前会话
//            session.invalidate();
        }
    }}

    // 处理POST请求,执行和处理GET请求一样的方法
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    
    // 判断传入的值是否为数字类型
    public static boolean isNumeric(final String str) {
        // null or empty
        if (str == null || str.length() == 0) {
            return false;
        }

        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            try {
                Double.parseDouble(str);
                return true;
            } catch (NumberFormatException ex) {
                try {
                    Float.parseFloat(str);
                    return true;
                } catch (NumberFormatException exx) {
                    return false;
                }
            }
        }
    }
}

CInfo.java:(这个类其实没有必要做,直接返回一个字符串到session中即可,但这是一个时代留下来的经典用法,简单熟悉一下,算是回顾吧)

package exec;

//提示信息类
public class CInfo {
    // 提示信息
    private String info;

    // 提示信息的设置函数
    public void setInfo(String str) {
        info = str;
    }

    // 提示信息的读取函数
    public String getInfo() {
        return info;
    }
}

 

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>webapp</display-name>
    <servlet>
        <description>
        </description>
        <display-name>
        Action</display-name>
        <servlet-name>Action</servlet-name>
        <servlet-class>
        Action</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Action</servlet-name>
        <url-pattern>/Action</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

-----------servlet+jsp  end ---------

链接https://github.com/tangdiao/Explore/

 

  相关的基础和高深的知识,其他博客已有更新,这里不再赘述。其实servlet+jsp很可爱,加上数据库访问层,对于一般的web应用完全没问题。从实际项目开发中,我发现,一个应用要真正的落地,构建应用阶段其实问题不大,不断地update就行了。重在后面维护和运营等一系列事情,这里涉及到一个软件的整个生命周期的问题。我是正在探索中的小白,一起加油吧。

推荐阅读