Skip to content

Commit

Permalink
Merge pull request #300 from qiuwenqiol/master
Browse files Browse the repository at this point in the history
#4 提交实验代码
  • Loading branch information
zengsn authored Oct 28, 2016
2 parents f0e5fcc + c655e4a commit 3cf2b7c
Show file tree
Hide file tree
Showing 3 changed files with 472 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package edu.hzu.javaweb.labs.se1414080902119;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
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;

@WebServlet("/1414080902119")
public class se1414080902119servlet extends HttpServlet {
private static final long serialVersionUID = 1L;

//初始化基本数据
private static List<BookType> types;
static{
types=new ArrayList<>();
types.add(new BookType("数学", false));
types.add(new BookType("文学",false));
types.add(new BookType("天文学",true));
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json;charset=utf-8");
request.setCharacterEncoding("utf-8");
String operate=request.getParameter("operate");
if(operate==null)operate="";
switch (operate) {
case "add":
addTypes(response,request);
break;
case "delete":
deleteTypes(response,request);
break;
default: showList(response);
break;
}

}

private void deleteTypes(HttpServletResponse response, HttpServletRequest request) throws IOException {
// TODO Auto-generated method stub
String name=request.getParameter("name");
String str="";
System.out.println(name);
String[]arr=name.split(",");
for(int j=0;j<arr.length;j++)
{
for(int i=0;i<types.size();i++){
BookType type=types.get(i);
String typeName=type.getName();
if(typeName.equals(arr[j]))
{
if(type.getIsHasBook()==false)
types.remove(type);
else str+=type.getName()+",";
break;
}
}
}
if(str.length()>1){
str=str.substring(0, str.length()-1);
}
PrintWriter writer = response.getWriter();
if(str.length()==0){
writer.write("{\"msg\":\"success\"}");
}
else writer.write("{\"msg\":\""+str+" 类别下有图书,不可删除"+"\"}");

writer.close();
}

public void showList(HttpServletResponse response) throws IOException
{
PrintWriter writer = response.getWriter();
writer.write(reJSON(types));
writer.close();
}

public void addTypes(HttpServletResponse response,HttpServletRequest request)throws IOException
{
String name=request.getParameter("name");
PrintWriter writer = response.getWriter();
if(name==null||name.equals(""))
{
writer.write("{\"msg\":\"请输入图书类别\"}");
writer.close();
return ;
}
for(int i=0;i<types.size();i++){
if(types.get(i).getName().equals(name))
{
writer.write("{\"msg\":\"该图书类别存在\"}");
writer.close();
return ;
}
}
BookType type=new BookType();
type.setName(name);
type.setIsHasBook(false);
types.add(type);
writer.write("{\"msg\":\"success\"}");
writer.close();
}

/**
* 返回JSON数据
* @param list 图书类别集合
* @return 图书类别的JSON数据
*/
public String reJSON(List<BookType> list)
{
StringBuffer str=new StringBuffer("{");
Iterator<BookType> iterator = list.iterator();
while(iterator.hasNext())
{
BookType bookType = iterator.next();
str.append("\""+bookType.getName()+"\":\""+bookType.getIsHasBook()+"\"");
if(iterator.hasNext())str.append(",");
}
str.append("}");
return str.toString();
}
}
//图书类别类
class BookType
{
private String name; //图书类别名字
private Boolean isHasBook; //该类别下是否有图书标志
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getIsHasBook() {
return isHasBook;
}
public void setIsHasBook(Boolean isHasBook) {
this.isHasBook = isHasBook;
}
public BookType(String name, Boolean isHasBook) {
super();
this.name = name;
this.isHasBook = isHasBook;
}
public BookType() {
super();
}
}
85 changes: 85 additions & 0 deletions jweb/web/1414080902119/client.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<%@ 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>
<title>Insert title here</title>
<link rel="stylesheet"
href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
<link rel="stylesheet"
href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<script type="text/javascript">
var str = null;
var url="../1414080902119";
var aBtn = document.getElementsByClassName('btn1 btn btn-default button');
var aDiv=document.getElementsByClassName("hehe");
window.onload = function() {
aBtn[0].onclick=function()
{
ajax(url,addElement);
}
}
/*
* 将json数据转化为标签
*/
function addElement(json)
{
var oDiv=document.getElementById("top");
oDiv.innerText="";
for(var a in json)
{
var oA=document.createElement("a");
oA.className="list-group-item";
oA.innerText=a;
oDiv.appendChild(oA);
}
}
/*
* url请求地址
* doSome方法对json数据进行处理
* ajax获取数据
*/
function ajax(url,doSome,data) {
var xmlHttpRequest = null;
try {
xmlHttpRequest = new XMLHttpRequest();
} catch (e) {
xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlHttpRequest.open('post', url+'?' + new Date().getTime(),
true)
xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttpRequest.send(data);
xmlHttpRequest.onreadystatechange = function() {
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
str = xmlHttpRequest.responseText;
var json=eval('('+str+')')
doSome(json);
} else {
alert("出错了");
}
}
}
}
</script>
<body>
<div style="width: 380px; margin: 0 auto; margin-top: 50px;" id="main"
align="center">
<div class="btn-group btn-group-justified">
<div class="btn-group">
<button id="btn1" type="button" class="btn1 btn btn-default button">查看图书类别</button>
</div>
</div>
<div class="hehe"
style="width: 380px; height: 200px; border: 1px solid #000; border-radius: 5px">
<div class="list-group" id="top">
</div>
</div>
</div>
</body>
</html>
Loading

0 comments on commit 3cf2b7c

Please sign in to comment.