软工作业4—web版四则运算
大家好,今天很高兴给大家分享这次作业。
结队成员:袁雪峰和陈岩。
分工:前端各做了一个界面,一个是第一版一个是第二版。后端是讨论交流一起做出来的,用的是第一次作业的内核。
项目见GitHub:https://github.com/a1171404817/RuangongWork4.git
目录:
前端界面(第一版):
login.jsp
main.jsp
showr.jsp
前端界面(第二版):
感觉第一版的前端不是很完美,就稍微改了改前端,主要用了bootstrap技术,又插了一些图片
login.jsp
main.jsp
showr.jsp
部分代码:
mainServlet.java
package Servlet;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import DB.db;
import model.User;
import sizeyunsuan.*;
public class mainServlet extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res) throws UnsupportedEncodingException,IOException
{
req.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
HttpSession session=req.getSession();
int shuliang=Integer.valueOf(req.getParameter("shuliang"));
int yunsfn=Integer.valueOf(req.getParameter("yunsfn"));
int zuojie=Integer.valueOf(req.getParameter("zuojie"));
int youjie=Integer.valueOf(req.getParameter("youjie"));
String[] havafx=req.getParameterValues("vehicle");
boolean havacc=false;
boolean havakh=false;
if(havafx==null)
{
havacc=false;havakh=false;
}
else if(havafx.length==2)
{
havacc=true;havakh=true;
}
else if(havafx.length==1)
{
if(havafx[0].equals("havacc"))
{
havacc=true;havakh=false;
}
else
{
havacc=false;havakh=true;
}
}
ArrayList<ArrayList<String>> als=new init().initString(shuliang,yunsfn,zuojie,youjie,havacc,havakh);
session.setAttribute("als", als);
res.sendRedirect("main.jsp");
}
public void doPost(HttpServletRequest req,HttpServletResponse res)throws UnsupportedEncodingException,IOException
{
doGet(req,res);
}
}
showresultServlet.java
package Servlet;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import DB.db;
import model.User;
import sizeyunsuan.*;
import java.lang.*;
public class showresultServlet extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res) throws UnsupportedEncodingException,IOException
{
req.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
HttpSession session=req.getSession();
ArrayList<ArrayList<String>> als=(ArrayList<ArrayList<String>>)session.getAttribute("als");
ArrayList<String> userAnswer=new ArrayList<String>();
for(int i=0;i<als.size();i++)
{
userAnswer.add(req.getParameter("fname"+i));
}
session.setAttribute("userAnswer", userAnswer);
ArrayList<String> duiCuo=new ArrayList<String>();
int fenzi=0;
for(int i=0;i<als.size();i++)
{
if(als.get(i).get(1).equals(userAnswer.get(i)))
{
duiCuo.add("√");
fenzi++;
}
else {
duiCuo.add("×");
}
}
session.setAttribute("duiCuo", duiCuo);
ArrayList<Double> fenshu=new ArrayList<Double>();
fenshu.add((double) (((double)fenzi/(double)als.size())*100));
session.setAttribute("fenshu", fenshu);
res.sendRedirect("showr.jsp");
}
public void doPost(HttpServletRequest req,HttpServletResponse res)throws UnsupportedEncodingException,IOException
{
doGet(req,res);
}
}
main.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@page import="model.LyTable" %>
<%@page import="DB.db" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'main.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body {
background-image: url('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1554809783749&di=21b4c919da2ab83521c10886808ce8fa&imgtype=0&src=http%3A%2F%2Fimg18.3lian.com%2Fd%2Ffile%2F201706%2F24%2Fc4b00fdd1248ede73d5fc461f61f895f.jpg');
background-repeat: repeat;
margin-left: 450px;
margin-top: 0px;
}
</style>
</head>
<body bgcolor=""white"">
<form action="showresultServlet" method="post">
<table border="1">
<caption>Problems board</caption>
<tr>
<th>Problem</th>
<th>Blank</th>
<%
ArrayList<ArrayList<String>> als=(ArrayList<ArrayList<String>>)session.getAttribute("als");
for(int i=0;i<als.size();i++){
ArrayList<String> tmal=(ArrayList<String>) als.get(i);
%>
<tr>
<td><%=tmal.get(0)%></td>
<td><input type="text" name="fname<%=i%>" /></td>
</tr>
<%
}
%>
</table>
</br>
<input type="submit" value="Sumbit" style=" margin-left:125px;width:150px;height:40px;"/>
</form>
<%
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String start = df.format(d);
session.setAttribute("start",start);
%>
<div style="padding-left:50px">
Starting time:<%=start %>
</div>
</body>
</html>
更多进项目查看
几个偷懒的地方(以后有空再完善):
1,偷个懒没有用到数据库(算式和结果用的一个嵌套动态数组:Arraylist<Arraylist<String>>装的,放在session里面前后台用的)。
,ajax部分更新、输入的答案的两端空格修剪(因为是字符串相等)、
2,运算符值不能太多、因为运算符数组在sizie10以内;数值不能过大,因为会导致结果溢。
感受和反思:
1,用JavaServlet把后台写的代码串起来了,让自己的代码可视化,真正可以起作用(因为谁也不会去用命令提示窗做题)。
感觉很棒。
2,前后台数据交互比较繁琐,因为不会用ajax技术,一直在用session作为数据交换的容器,比较费事,前后台交互的时候经常出现获取不到数的情况,花了很长时间才把这个问题解决。以后应该增强这方面的技术。
3,一个好的同伴的重要性,很多问题都可以讨论、交流,一起朝着一个目标做,比一个人闷头做要感觉好太多了!这些非常感谢一个小组内的伟光、陈岩、还有刘松同学。