1、 笔记本电脑开wifi使用Tomcat9做服务器。(默认150个链接)
2、 在mysql数据库中录入两个班级的学生信息。
3、 登陆界面,对两个班级学生username、password验证。有管理员权限(统计)。
4、 数据采集界面,设计表单填写。
5、 Servlet调用Add方法录入数据库。
6、 统计在数据库做相关查询,与界面进行交互。
分为login.jsp、login_action.jsp、grade.jsp和Answer.jsp
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
登陆界面
<%@ include file="top.jsp"%>
<%@ include file="footer.jsp"%>
login_action.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
Insert title here
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("username");
String pass = request.getParameter("password");
//登陆校验 成功则进入评分表单;失败则停留登陆界面。
boolean flag = Conn.verify(id, pass);
if(flag){
session.setAttribute("Username", id);
response.sendRedirect("grade.jsp");
}
else if(id.equals("10000")&&pass.equals("10000"))
response.sendRedirect("Answer.jsp");
else
response.sendRedirect("login.jsp");
%>
foot.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
top.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
grade.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<% String path = request.getContextPath(); %>
在线评分
<%-- <% String str = (String)session.getAttribute("Username");
out.print(str+"1111111111111");
%> --%>
<%@ include file="footer.jsp"%>
Answer.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
String path = request.getContextPath();
%>
addServlet.servlet
package Com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Com.Dao.stuDao;
import Com.Dao.stuDaoimpl;
import Com.Entity.Student;
/**
* Servlet implementation class tjServlet
*/
@WebServlet("/addServlet")
public class addServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public addServlet() {
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("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
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
HttpSession session = request.getSession();
String username = (String)session.getAttribute("Username");
// System.out.println(username);
String userid = (String)request.getParameter("userid");
int ppt = Integer.parseInt(request.getParameter("ppt").toString());
int speech = Integer.parseInt(request.getParameter("speech").toString());
int answer = Integer.parseInt(request.getParameter("answer").toString());
int team = Integer.parseInt(request.getParameter("team").toString());
int finish = Integer.parseInt(request.getParameter("finish").toString());
Student stu = new Student(username,ppt,speech,answer,team,finish);
stuDao s = new stuDaoimpl();
s.Add(stu,userid);
response.sendRedirect("success.jsp");
}
}
stuDaoimpl.java
package Com.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import Com.DB.Conn;
import Com.Entity.Student;
public class stuDaoimpl implements stuDao {
Connection conn = null;
PreparedStatement pre = null;
ResultSet rs = null;
Statement state = null;
@Override
public List Select() {
List list = new ArrayList();
String sql = "SELECT DISTINCT tb_stu.username,tb_stu.stuName FROM tb_stu WHERE tb_stu.username NOT IN (SELECT tb_grade.username FROM tb_grade)";
conn = Conn.getConnection();
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
Student stu = new Student(rs.getString("username"), rs.getString("stuName"));
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public List Search() {
// TODO Auto-generated method stub
List list = new ArrayList();
String sql = "SELECT userid as 学号,tb_stu.stuName as 姓名,AVG(tb_grade.Sum) as 平均分 FROM tb_grade,tb_stu WHERE tb_grade.userid = tb_stu.username GROUP BY tb_grade.userid";
conn = Conn.getConnection();
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
Student stu = new Student(rs.getString("学号"), rs.getString("姓名"), rs.getFloat("平均分"));
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public void Add(Student stu, String userid) {
// TODO Auto-generated method stub
int i = 0;
String username = stu.getStuNum();
int ppt = stu.getPpt();
int speech = stu.getSpeech();
int answer = stu.getAnswer();
int team = stu.getTeam();
int finish = stu.getFinish();
String sql1 = "Select Count(*) from tb_grade where username = ? and userid = ?";
String sql2 = "insert into tb_grade values(1," + username + "," + userid + "," + ppt + "," + speech + ","
+ answer + "," + team + "," + finish + ","+"null"+")";
try {
conn = Conn.getConnection();
pre = conn.prepareStatement(sql1);
pre.setObject(1, username);
pre.setObject(2, userid);
rs = pre.executeQuery();
while (rs.next()) {
i = rs.getInt(1);
}
// 不允许重复评分
if (i == 0)
{ conn = Conn.getConnection();
pre = conn.prepareStatement(sql2);
pre.execute();
}
// 插入分数
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getErrorCode());
}
}
}
这里使用的是Tomcat9,到官网下载。
步骤:
1、 将文件打包成war文件(ecilipse中export)
2、 将war文件复制到tomcat9 中webapps
3、 将tomcat启动
4、 将防火墙关闭
5、 打开360wifi
6、 Cmd管理员模式 ipconfig/all 查看ipv4地址
7、 手机连接登陆网址:ip地址+“:8080/Login/login.jsp”
这里使用的是mysql5.7,也是到官网下载。还有navicat,最新版本即可。(附mysql数据库安装指南)
注意数据库连接时,导入mysql-connector-java-5.1.26-bin.jar
mysql表
tb_stu表
tb_grade表
id流水号和Sum求加权值的触发器:
CREATE TRIGGER tr_ID BEFORE INSERT ON tb_grade
FOR EACH ROW
BEGIN
DECLARE n int;
SELECT IFNULL(MAX(id),0) INTO n FROM tb_grade;
SET NEW.id = n + 1;
SET NEW.Sum = (NEW.ppt*0.2+NEW.speech*0.4+NEW.answer*0.2+NEW.team*0.1+NEW.finish*0.1);
END;
用户登陆界面
该登录界面是使用mobileSelect.js做的。考虑下手机端的适配。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
评分界面
<%-- <%
String str = (String) session.getAttribute("Username");
if (str == null || str.equals(""))
response.sendRedirect("login.html");
%> --%>
<%
String path = request.getContextPath();
%>