java第三阶段(jsp/java web)考试

转眼间从辞职到学习java已经4个月了,3个阶段的学习,究竟吸收了多少呢?

这次考试勉强算是完成了基本功能,不过也如同老师说的一样:这题目这么简单,根本不需要思路,只是最基本的增删查改,1个多小时怎么会还有人写不完?

虽然自己算是写完了,不过似乎也存在着些问题,最明显的是对时间格式感到很陌生,最后显示的时间格式多出了时分秒,并且不知道如何去除.

好了,马上就要到第四阶段了,继续加油吧

把考试内容回答梳理一下,算是java web学习的一个阶段性总结吧


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    图书管理系统
	
   
   
	
   
   
	
   
       
	
   
   
	
   
   
	
   
   

  
  
   
   
    
    <% response.sendRedirect("book?do=show"); %>
   
   
  
  
  

图书信息

序号图书名称图书作者购买时间图书分类操作
${status.count }${book.name }${book.author }${book.times }${book.typs }修改 删除
<script> function checkdel(id){ var flag=confirm("确定要删除吗?"); if(flag){ return true; } else return false; } </script>
这是首页

----------------------------------------------------------



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'addbook.jsp' starting page
    
	
   
   
	
   
   
	
   
       
	
   
   
	
   
   
	
   
   

  
  
  
  

添加图书信息

图书名称:
图书作者:
购买日期:
图书类别:
<script> function check(){ var a=document.getElementById("a").value; var b=document.getElementById("b").value; var c=document.getElementById("c").value; var d=document.getElementById("d").value; if(a==""||b==""||c==""||d==""){ alert("请填写完整后提交!"); return false; } else return true; } </script>
添加图书页面

----------------------------------------------------------

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'addbook.jsp' starting page
    
	
   
   
	
   
   
	
   
       
	
   
   
	
   
   
	
   
   

  

  
  

修改图书信息

图书名称:
图书作者:
购买日期:
图书类别:
<script> function check(){ var a=document.getElementById("a").value; var b=document.getElementById("b").value; var c=document.getElementById("c").value; var d=document.getElementById("d").value; if(a==""||b==""||c==""||d==""){ alert("请填写完整后提交!"); return false; } else return true; } </script>
编辑图书页面

----------------------------------------------------------


package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.BookDao;
import dao.impl.BookDaoImpl;
import entity.Book;

public class BookServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session=request.getSession();
		String dos=request.getParameter("do");
		BookDao bd=new BookDaoImpl();
		
		//显示图书列表
		if("show".equals(dos)){
			List
   
   
    
     blist=bd.getAllBook();
			session.setAttribute("blist", blist); //图书列表
			response.sendRedirect("index.jsp");
			return;
		}
		
		//添加图书
		if("addbook".equals(dos)){
			String name=request.getParameter("name");
			String author=request.getParameter("author");
			String times=request.getParameter("times");
			String typs=request.getParameter("typs");
			Book b=new Book();
			b.setName(name);
			b.setAuthor(author);
			b.setTimes(times);
			b.setTyps(typs);
			int add=bd.addBook(b);
			if(add>0){
				out.print("<script>alert('新增图书成功!');location.href='book?do=show'</script>");
				return;
			}
			else{
				out.print("<script>alert('新增图书失败!');location.href='book?do=show'</script>");
				return;
			}
		}
		//修改图书前页面
		if("edit".equals(dos)){
			String id2=request.getParameter("id");
			int id=Integer.parseInt(id2);
			Book b=bd.findBookById(id);
			session.setAttribute("book", b);
			response.sendRedirect("editbook.jsp?id="+id);
			return;
		}
		
		
		//修改图书
		if("editbook".equals(dos)){
			String id2=request.getParameter("id");
			int id=Integer.parseInt(id2);
			String name=request.getParameter("name");
			String author=request.getParameter("author");
			String times=request.getParameter("times");
			String typs=request.getParameter("typs");

			Book b=new Book();
			b.setId(id);
			b.setName(name);
			b.setAuthor(author);
			b.setTimes(times);
			b.setTyps(typs);
			int edit=bd.editBook(b);
			System.out.println(edit);
			if(edit>0){
				out.print("<script>alert('修改图书成功!');location.href='book?do=show'</script>");
				return;
			}
			else{
				out.print("<script>alert('修改图书失败!');location.href='book?do=show'</script>");
				return;
			}
		}
		
		
		//删除图书
		if("delbook".equals(dos)){
			String id2=request.getParameter("id");
			int id=Integer.parseInt(id2);
			int del=bd.delBook(id);
			if(del>0){
				out.print("<script>alert('删除图书成功!');location.href='book?do=show'</script>");
				return;
			}
			else{
				out.print("<script>alert('删除图书失败!');location.href='book?do=show'</script>");
				return;
			}
		}
	}

}

   
   

servlet页面

----------------------------------------------------------

package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.BaseDao;
import dao.BookDao;
import entity.Book;

public class BookDaoImpl extends BaseDao implements BookDao {

	@Override
	public List
    
    
     
      getAllBook() {
		String sql = "select * from book order by times desc";
		List
     
     
      
       blist = new ArrayList
      
      
       
       ();
		ResultSet rs = executeQuery(sql);
		try {
			while (rs.next()) {
				Book b = new Book(rs.getInt(1), rs.getString(2),
						rs.getString(3), rs.getString(4), rs.getString(5));
				blist.add(b);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll();
		}
		return blist;
	}

	@Override
	public int addBook(Book b) {
		String sql="insert into book values(book_seq.nextval,?,?,to_date(?,'yyyy-mm-dd'),?)";
		int num=executeUpdate(sql, b.getName(),b.getAuthor(),b.getTimes(),b.getTyps());
		return num;
	}

	@Override
	public int editBook(Book b) {
		String sql="update book set name=?,author=?,times=to_date(?,'yyyy-mm-dd'),typs=? where id=?";
		int num=executeUpdate(sql,b.getName(),b.getAuthor(),b.getTimes(),b.getTyps(),b.getId());
		return num;
	}

	@Override
	public Book findBookById(int id) {
		String sql = "select * from book where id=?";
		Book b=null;
		ResultSet rs = executeQuery(sql,id);
		try {
			if (rs.next()) {
				 b = new Book(rs.getInt(1), rs.getString(2),
						rs.getString(3), rs.getString(4), rs.getString(5));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll();
		}
		return b;
	}

	@Override
	public int delBook(int id) {
		String sql="delete from book where id=?";
		int num=executeUpdate(sql,id);
		return num;
	}

}

      
      
     
     
    
    
bookimpl实现类

----------------------------------------------------------



就先这样吧


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值