軟工作業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,一個好的同伴的重要性,很多問題都可以討論、交流,一起朝着一個目標做,比一個人悶頭做要感覺好太多了!這些非常感謝一個小組內的偉光、陳巖、還有劉松同學。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章