登录页面(index.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+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> 大数据分类系统</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="keywods" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="style.css">
-->
<style >
body{
background-image:url(images/bank.jpg);
text-align:center;
}
#btn {
background-image:url(images/button1.png);
width:100px;
height:35px;
border:0;
}
</style>
<script language='javascript'>
function on_submit(){
if(form.pId.value==""){
alert("帐号不能为空,请输入!");
form.pId.focus();
return false;
}
if(form.password.value==""){
alert("密码不能为空,请输入!");
form.password.focus();
return false;
}
}
</script>
</head>
<%session.setAttribute("Person",null);%>
<body>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<form method="post" name="form" action="login?command=login" onsubmit="return on_submit()">
<table width='550' align='center' border='1' cellPadding='5'>
<tr><td width='550' height='50' colspan='2' align='center'><font color='white' size="5">大数据分类</font></td>
<tr>
<td width="200" height='50' align='center'><font color="white" size="3">帐号:</font></td>
<td width="350" height='50' align='center'><input type='text' name='pId' value='20132029' style="width:155px;"></td>
<tr>
<td width="200" height='50' align='center'><font color="white" size="3">密码:</font></td>
<td width="200" height="50" align="center"><input type='password' name='password' value="20132029" style="width:155px;"></td>
<tr>
<td width="550" height='40' colspan="2" align="center">
<input type="submit" value="登录" id="btn">
</td>
<tr>
<td width="550" height='40' colspan='2' align="center">
<font size="3"><%=session.getAttribute("tip1")==null?"请登录!":session.getAttribute("tip1") %></font>
</td>
</table>
</form>
<%request.setCharacterEncoding("UTF-8"); %>
</body>
</html>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ClassifyProject</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.SWJTU.Classify.servlet.logControl</servlet-class>
</servlet>
<servlet>
<servlet-name>calculate</servlet-name>
<servlet-class>com.SWJTU.Classify.servlet.calculateControl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>calculate</servlet-name>
<url-pattern>/calculate</url-pattern>
</servlet-mapping>
</web-app>
登录控制的servlet(loginControl.java)
package com.SWJTU.Classify.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.SWJTU.Classify.bean.Person;
import com.SWJTU.Classify.dao.PersonDao;
import com.solidisc.web.ModuleController;
/**
* Servlet implementation class logControl
*/
@WebServlet("/logControl")
public class logControl extends ModuleController {
protected void actionlogin(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
String pId = req.getParameter("pId");
String password = req.getParameter("password");
Person person = new Person();
PersonDao pDao = new PersonDao();
person = pDao.getId(pId);
if(person ==null ||person.getStatus().equals(1)){
req.getSession(true).setAttribute("tip1","该帐号不存在!");
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
else{
if(person.getPassword().equals(password)){
HttpSession session = req.getSession();
session.setAttribute("Person",person);
req.getSession(true).setAttribute("tip1","请登录!");
resp.sendRedirect("main.jsp");
}
else{
req.getSession(true).setAttribute("tip1","密码错误!");
System.out.println(person.getPassword());
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
}
}
mysql数据库链接文件(Conn.java)
package com.SWJTU.Classify.dbco;
import java.sql.Connection;
import java.sql.DriverManager;
public class Conn {
public static Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
}catch (Exception e1) {
System.out.println("数据库驱动加载出错");
}
try {
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/Classify?useUnicode=true&characterEncoding=gbk","root","123");
}catch (Exception e2) {
System.out.println("数据库链接错误!");
}
return conn;
}
}
是数据库抽象文件(Person.java)
package com.SWJTU.Classify.bean;
public class Person {
private String pId;
private String password;
private Integer status;
public String getpId() {
return pId;
}
public void setpId(String pId) {
this.pId = pId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
数据库操作文件(PersonDao)
package com.SWJTU.Classify.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.SWJTU.Classify.bean.Person;
import com.SWJTU.Classify.dbco.Conn;
public class PersonDao {
public Person getId(String pId){
Person person =null;
Connection conn = Conn.getConn();
ResultSet rs=null;
String sql = "select * from Person where pId ='"+pId+"'and status=0";
try{
Statement st = conn.createStatement();
rs = st.executeQuery(sql);
if(rs.next()){
person = new Person();
person.setpId(rs.getString(1));
person.setPassword(rs.getString(2));
person.setStatus(rs.getInt(3));
}
}
catch(SQLException e){
e.printStackTrace();
}
return person;
}
}
是文件上传页面(main.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="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<style>
.btn{
width:120px;
height:30px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
if(session.getAttribute("Person")==null){
request.setAttribute("tip1","请登录");
response.sendRedirect("index.jsp");
}
%>
<body>
<p> </p>
<p> </p>
<form method="post" name="form1" action="calculate?command=calculate" enctype="multipart/form-data" onsubmit="return on_submit()">
<table width='550' align='center' border='1' cellPadding='5'>
<tr>
<td width='200' height='50' align='center'>训练数据集</td>
<td width='350' height='50' align='center'><input type="file" name='trainfile' id="f_trainfile"/></td>
<tr>
<td width='200' height='50' align='center'>数据集描述文件</td>
<td width='350' height='50' align='center'> <input type="file" name='namefile' id="f_namefile"/></td>
<tr>
<td width='200' height='50' align='center'>测试数据集</td>
<td width='350' height='50' align='center'><input type="file" name='testfile' id="f_testfile"/></td>
<tr>
<td width='200' height='50' align='center'>分类算法种类</td>
<td width='350' height='50' align='center'>
<select name="searchType" >
<option value="Bayes">贝叶斯算法</option>
<option value="k-nearest">K-最近邻算法</option>
<option value="Based-on-gravity">基于引力算法</option>
</select>
</td>
<tr>
<td width="200" height='40' align='center'>
<a href="calculate?command=recharge"><button type="button" class="btn">重置</button></a>
</td>
<td width="350" height='40' align='center'>
<input type="submit" value="计算" class='btn'>
</td>
</table>
</form>
</body>
</html>
上传控制servlet(calculateControl.java)
package com.SWJTU.Classify.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.solidisc.web.ModuleController;
/**
* Servlet implementation class calculateControl
*/
@WebServlet("/calculateControl")
public class calculateControl extends ModuleController {
protected void actioncalculate(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException{
//记录文件名
String fileName[] =new String[3];
//记录文件次序
int i=0;
String searchType = req.getParameter("searchType");
//得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界访问。
//String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
String savePath = new String("/home/wangshengwu/upload");
System.out.print( "路径是:"+savePath);
File file = new File(savePath);
//判断上传文件的保存目录是否存在
if(!file.exists()&&!file.isDirectory()){
//创建目录
file.mkdir();
}
//消息提示
String message = "";
try{
//使用Apache文件上传组件处理文件上传步骤
//1、创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//2、创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
//3、判断提交上来的数据是否是上传表单的数据
if(!ServletFileUpload.isMultipartContent(req)){
return;
}
//4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>
List<FileItem> list = upload.parseRequest(req);
for(FileItem item :list){
//如果是fileitem中封装的是普通输入项的数据
if(item.isFormField()){
String name = item.getFieldName();
//解决普通输入项的数据的中文乱码问题
String value = item.getString("UTF-8");
//value = new String(value.getBytes("iso8859-1"),"UTF-8");
System.out.println(name +"=" +value);
}else{//如果fileitem中封装的是上传文件
//得到上传的文件名称
String filename = item.getName();
fileName[i++]=filename;
System.out.println(filename);
if(filename==null||filename.trim().equals("")){
continue;
}
//注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的
//若带有路径,处理获取到的上传文件的文件名的路径部分,只保留文件名
filename = filename.substring(filename.lastIndexOf("\\")+1);
//获取item中的上传文件的输入流
InputStream in = item.getInputStream();
//创建一个文件输出流(如果是linux系统,此处的"\\"要改成"/")
FileOutputStream out = new FileOutputStream(savePath+ "\\"+filename);
//创建一个缓冲区
byte buffer[] = new byte[1024];
//判断输入流中的数据是否已经读完的标志
int len = 0;
//循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in 里面还有数据
while((len=in.read(buffer))>0){
//使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath+ "\\"+filename)
out.write(buffer,0,len);
}
//关闭输入流
in.close();
//关闭输出流
out.close();
//删除处理文件上传时生成的文件
item.delete();
message = "文件上传成功!";
}
}
}catch(Exception e){
message = "文件上传失败!";
e.printStackTrace();
}
req.setAttribute("message",message);
req.getRequestDispatcher("message.jsp").forward(req, resp);
}
protected void actionrecharge(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException{
}
}