使用的主要技术有:
JavaEE、JDBC、AJAX、JSP、JavaBean
本项目开发环境为:
下载完工程文件,按照下面的数据库结构截图建立好数据库的表,然后在 NewsRealeseDao.java中配置好相关的数据库URL、用户名和密码应该就可以直接运行起来了。
数据库配置文件下载地址:https://github.com/muziyongshixin/JavaWeb-Documents-Management-System/blob/master/MyNews.sql
为了节省篇幅这里主要介绍JSP中负责和Servlet或后台DAO有关的代码。
登录界面:
主要的就是form标签里的action属性,表示将表单里的内容提交给后台的checkLogin_user这个Servlet进行处理,其中input标签里的name属性标记其中的值,可以在Servlet中使用request.getparameter()方法得到标签中填入的值。
(还有一点需要注意的是action里的方法需要先在web.xml文件中进行注册,这样tomcat服务器才能正确的找到对应的类进行后续的处理,实际上所有的Servlet类都需要在web.xml中进行注册,所以后面就不再赘述这个问题了)
处理登录的Servlet:
处理JSP发送过来的数据,调用后台程序进行处理,并返回结果。
package servlet;
import dao.NewsRealeseDao;
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 java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class checkLogin_user extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//这里的"user"和"pass"必须要和JSP里的对应的标签的name属性相同
String user=request.getParameter("user");
String pass=request.getParameter("pass");
NewsRealeseDao newsRealeseDao=new NewsRealeseDao();
try {
boolean checked=newsRealeseDao.ischecked(user,pass,"user");
//调用后台的Dao方法利用user表进行身份验证
if(checked)
{
HttpSession session=request.getSession();
session.setAttribute("username",user);//设置用户的姓名
response.sendRedirect("content_user.jsp");
}
else
{
response.sendRedirect("login_user.jsp");
}
}
catch (Exception ex)
{
Logger.getLogger(checkLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description"+"public String getServletInfo() ";
}
}
数据库访问类newsRealeseDao:
访问数据库并提供相关的方法。
import com.lut.beans.NewsRealese;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
public class NewsRealeseDao {
public static String driver = "com.mysql.jdbc.Driver";//定义驱动
public static String url = "jdbc:mysql://localhost:3306/myNews?useUnicode=true&characterEncoding=utf-8";//定义URL
public static String databseUser = "root";//定义用户名
public static String password = "root";//定义密码
private ArrayList getNews(Statement stat, String sql)//处理具体的新闻查询请求,返回所有结果
{
ArrayList newsRealese = new ArrayList();
try {
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) { //实例化VO
NewsRealese news = new NewsRealese();
news.setNewsId(rs.getString("newsid"));
news.setContent(rs.getString("content"));
news.setHead(rs.getString("head"));
news.setIssueuser(rs.getString("issueuser"));
news.setPublish_time(rs.getString("publish_time"));
news.setNewstype(rs.getString("newstype"));
newsRealese.add(news);
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
return newsRealese;
}
}
public ArrayList UserQueryAllNews(String username, String table) throws Exception {//用户查看自己所有的文章
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//运行SQL语句
String sql = "select * from " + table + " where issueuser='" + username + "' order by publish_time desc";
Statement stat = conn.createStatement();
newsRealese = getNews(stat, sql);
if (newsRealese.size() == 0) {
System.out.println("查询不到任何信息============");
return null;
}
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return newsRealese;
}
}
public ArrayList UserQueryOthersNews(String username) throws Exception {//查看自己可见的其他人的所有的文章
Connection conn = null;
ArrayList rt = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//运行SQL语句
String sql = "select * from news where newsId IN (select newsId from authority where username=?) order by publish_time desc";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
NewsRealese news = new NewsRealese();
news.setNewsId(rs.getString("newsid"));
news.setContent(rs.getString("content"));
news.setHead(rs.getString("head"));
news.setIssueuser(rs.getString("issueuser"));
news.setPublish_time(rs.getString("publish_time"));
news.setNewstype(rs.getString("newstype"));
rt.add(news);
}
if (rt.size() == 0) {
System.out.println("查询不到任何信息============Dao.UserQueryOthersNews");
return null;
}
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return rt;
}
}
public ArrayList AdministorQueryAllNews(String username) throws Exception {//管理员查看所有的文章
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//运行SQL语句
String sql = "select * from news order by publish_time desc";//获取
Statement stat = conn.createStatement();
newsRealese = getNews(stat, sql);
if (newsRealese.size() == 0) {
System.out.println("查询不到任何信息============");
return null;
}
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return newsRealese;
}
}
//查询一个消息
public ArrayList AdministorQueryCheckPending() throws Exception {//查询所有的待审核的文章
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
Statement stat = conn.createStatement();
String sql = "select * from check_pending order by publish_time desc";//获取newsid,使用?代替字符串,以免会发生错误
Statement st = conn.createStatement();
newsRealese = getNews(st, sql);
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
return newsRealese;
}
}
//用户删除数据
public String deleteOneNews(String newsid, String table) throws Exception {
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
String sql_move = "insert into dustbin select * from news where newsId='" + newsid + "'";
String sql_delete = "DELETE FROM " + table + " WHERE newsId='" + newsid + "'";//获取newsid,使用?代替字符串,以免会发生错误
Statement ps = conn.createStatement();
int rs_move = ps.executeUpdate(sql_move);
if (rs_move != 0) {
int rs = ps.executeUpdate(sql_delete);
if (rs == 0)
System.out.println("删除失败==================NewsrealeaseDao");
} else {
System.out.println("插入到dustbin出错=============NewsrealeaseDao");
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return newsRealese.toString();
}
}
public String AdmindeleteCheck_pendingNews(String newsid) throws Exception {
Connection conn = null;
int rs = 0;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
String sql_delete = "DELETE FROM check_pending WHERE newsId='" + newsid + "'";//获取newsid,使用?代替字符串,以免会发生错误
Statement ps = conn.createStatement();
rs = ps.executeUpdate(sql_delete);
if (rs == 0) {
System.out.println("删除失败==================NewsrealeaseDao");
return null;
}
else
return "成功删除"+rs;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
}
return null;
}
public int AuthorizeOneNews(ArrayList userlist, String newsid) {
int count = 0;
Connection conn = null;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
Statement stat = conn.createStatement();
for (int i = 0; i < userlist.size(); i++) {
String sql = "insert into authority VALUES(?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, newsid);
ps.setString(2, userlist.get(i));
System.out.println(ps.toString());
count += ps.executeUpdate();
}
System.out.println("成功添加" + count + "行");
stat.close();
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return count;
}
}
//插入数据
public int insertOneNews(HashMap addnews_list, String table) throws Exception {//插入一个新的新闻
Connection conn = null;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
Statement stat = conn.createStatement();
String sql = "insert into " + table + " VALUES(?,?,?,?,?,?)";//获取newsid,使用?代替字符串,以免会发生错误
PreparedStatement ps = conn.prepareStatement(sql);
// ps.setString(1, table);
ps.setString(1, addnews_list.get("newsid"));
ps.setString(2, addnews_list.get("head"));
ps.setString(3, addnews_list.get("content"));
ps.setString(4, addnews_list.get("publish_time"));
ps.setString(5, addnews_list.get("issueuser"));
ps.setString(6, addnews_list.get("newstype"));
System.out.println(addnews_list.get("newstype") + "===============" + ps.toString());
int i = ps.executeUpdate();
System.out.println("成功添加" + i + "行");
stat.close();
conn.close();
return i;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
}
return 0;
}
public HashMap queryOneNews(String newsid, String table) throws SQLException {
Connection connection = null;
HashMap rt = new HashMap<>();
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, databseUser, password);
String sql = "select * from " + table + " where newsId='" + newsid + "'";
Statement ps = connection.createStatement();
System.out.println("将要执行的=====" + sql);
ResultSet rs = ps.executeQuery(sql);
while (rs.next()) {
rt.put("newsid", rs.getString("newsId"));
rt.put("head", rs.getString("head"));
rt.put("content", rs.getString("content"));
rt.put("time", rs.getString("publish_time"));
rt.put("author", rs.getString("issueuser"));
rt.put("newstype", rs.getString("newstype"));
return rt;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.close();
}
return rt;
}
//更新数据
public int updateOneNews(HashMap addnews_list) throws Exception {
Connection conn = null;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//获取newsid,使用?代替字符串,以免会发生错误
String sql = "UPDATE news set head=?,content=?,publish_time=?,issueuser=?,newstype=? where newsId=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(6, addnews_list.get("newsid"));
ps.setString(1, addnews_list.get("head"));
ps.setString(2, addnews_list.get("content"));
ps.setString(3, addnews_list.get("publish_time"));
ps.setString(4, addnews_list.get("issueuser"));
ps.setString(5, addnews_list.get("newstype"));
System.out.println(ps.toString());
int i = ps.executeUpdate();
System.out.println("成功更新" + i + "行");
conn.close();
return i;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
}
return 0;
}
public boolean CheckNews(String newsid)//审核通过一片文章
{
String sql_insert = "insert into news select * from check_pending where newsId=?";
String sql_delete = "delete from check_pending where newsId=?";
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, databseUser, password);
PreparedStatement ps = con.prepareStatement(sql_insert);
ps.setString(1, newsid);
int result = ps.executeUpdate();
if (result != 0) {
ps = con.prepareStatement(sql_delete);
ps.setString(1, newsid);
int r = ps.executeUpdate();
if (r != 0)
return true;
else
return false;
} else
return false;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public boolean ResumeNews(String newsid)//审核通过一片文章
{
String sql_insert = "insert into news select * from dustbin where newsId=?";
String sql_delete = "delete from dustbin where newsId=?";
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, databseUser, password);
PreparedStatement ps = con.prepareStatement(sql_insert);
ps.setString(1, newsid);
int result = ps.executeUpdate();
if (result != 0) {
ps = con.prepareStatement(sql_delete);
ps.setString(1, newsid);
int r = ps.executeUpdate();
if (r != 0)
return true;
else
return false;
} else
return false;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public boolean ischecked(String user, String pass, String table) throws Exception {//登录验证 table表示从哪个表中查询
Connection conn = null;
//获取连接
Class.forName(driver);
String sql = "select password from " + table + " where username='" + user + "'";
System.out.println(sql);
try {
conn = DriverManager.getConnection(url, databseUser, password);//不安全
System.out.println("建立database连接");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
if (rs.getString("password").equals(pass))
return true;
else
return false;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return false;
}
private static void show(ArrayList in) {
for (int i = 0; i < in.size(); i++) {
NewsRealese tem = (NewsRealese) in.get(i);
System.out.println(tem.getHead() + "======" + tem.getContent() + "@@@" + tem.getIssueuser() + "=====" + tem.getPublish_time());
}
}
public static void main(String arg[]) {
HashMap addnews_list = new HashMap<>();
addnews_list.put("newsid", "1234567892");
addnews_list.put("head", "第2条测试");
addnews_list.put("content", "这个是第一条测试文章,里面的内容都是随便乱写的~亏大发老师教辅拉世纪东方拉开圣诞节福利卡士大夫拉克丝打开发送大六块腹肌");
addnews_list.put("publish_time", "2017-4-4/12:12:12");
addnews_list.put("issueuser", "木子勇士心");
addnews_list.put("newstype", "1");
NewsRealeseDao nd = new NewsRealeseDao();
ArrayList rs = nd.AdministorQueryCheckPending(); ///查询所有的待审核订单
show(rs);
} catch (Exception e) {
e.printStackTrace();
}
// nd.CheckNews("1234567890"); //审核一个订单
}
}
web.xml 配置文件:
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>checkLoginservlet-name>
<servlet-class>servlet.checkLoginservlet-class>
servlet>
<servlet>
<servlet-name>checkLogin_userservlet-name>
<servlet-class>servlet.checkLogin_userservlet-class>
servlet>
<servlet>
<servlet-name>checkNewsservlet-name>
<servlet-class>servlet.checkNewsservlet-class>
servlet>
<servlet>
<servlet-name>InsertOneNewsservlet-name>
<servlet-class>servlet.InsertOneNewsservlet-class>
servlet>
<servlet>
<servlet-name>DeleteOneNewsservlet-name>
<servlet-class>servlet.DeleteOneNewsservlet-class>
servlet>
<servlet>
<servlet-name>UpdateOneNewsservlet-name>
<servlet-class>servlet.UpdateOneNewsservlet-class>
servlet>
<servlet>
<servlet-name>QueryOneNewsservlet-name>
<servlet-class>servlet.QueryOneNewsservlet-class>
servlet>
<servlet>
<servlet-name>ShowAllNewsservlet-name>
<servlet-class>servlet.ShowAllNewsservlet-class>
servlet>
<servlet>
<servlet-name>QueryOneNews_userservlet-name>
<servlet-class>servlet.QueryOneNews_userservlet-class>
servlet>
<servlet>
<servlet-name>Authorizeservlet-name>
<servlet-class>servlet.Authorizeservlet-class>
servlet>
<servlet>
<servlet-name>ResumeNewsservlet-name>
<servlet-class>servlet.ResumeNewsservlet-class>
servlet>
<servlet-mapping>
<servlet-name>Authorizeservlet-name>
<url-pattern>/Authorizeurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>ShowAllNewsservlet-name>
<url-pattern>/ShowAllNewsurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>checkLoginservlet-name>
<url-pattern>/checkLoginurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>InsertOneNewsservlet-name>
<url-pattern>/InsertOneNewsurl-pattern>
servlet-mapping>
<filter>
<filter-name>EncodingFilterfilter-name>
<filter-class>com.lutsoft.filter.EncodingFilterfilter-class>
filter>
<filter-mapping>
<filter-name>EncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet-mapping>
<servlet-name>DeleteOneNewsservlet-name>
<url-pattern>/DeleteOneNewsurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>UpdateOneNewsservlet-name>
<url-pattern>/UpdateOneNewsurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>QueryOneNewsservlet-name>
<url-pattern>/QueryOneNewsurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>checkNewsservlet-name>
<url-pattern>/checkNewsurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>checkLogin_userservlet-name>
<url-pattern>/checkLogin_userurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>QueryOneNews_userservlet-name>
<url-pattern>/QueryOneNews_userurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>ResumeNewsservlet-name>
<url-pattern>/ResumeNewsurl-pattern>
servlet-mapping>
<session-config>
<session-timeout>
30
session-timeout>
session-config>
web-app>
下面简单介绍一下如何查看每个人的所有文章
实际上就是直接调用NewsRealeseDao.java中的方法来来直接查询数据库中的记录,然后将结果放到一个ArrayList中直接返回,然后在JSP页面上显示出来。
(查看别人已授权的文章方法类似,只是查询的时候增加一个条件而已)
JSP页面:
<table class="table table-hover">
<tr>
<th width="45">选择th>
<th width="300">标题th>
<th width="100">时间th>
<th width="100">类别th>
<th width="150">操作th>
tr>
<% String username = (String) session.getAttribute("username");
NewsRealeseDao newsRealeseDao = new NewsRealeseDao();
ArrayList newsRealese = newsRealeseDao.UserQueryAllNews(username,"news");
%>
<%
for (int i = 0; i < newsRealese.size(); i++) {
NewsRealese tem = (NewsRealese) newsRealese.get(i);
%>
<tr>
<td>
<input type="checkbox" name="id" value="<%=i%>>"/>
td>
<td><%=tem.getHead()%>
td>
<td><%=tem.getPublish_time()%>
td>
<td><%=tem.getNewstype()%>
td>
<td>
<a class="button border-green button-little" href="#"
onclick="queryInfo('<%=tem.getNewsId()%>','news')">详情a>
<a class="button border-blue button-little" href="#"
onclick="queryInfo('<%=tem.getNewsId()%>','news')">修改a>
<a class="button border-red button-little" href="#"
onclick="{if(confirm('确认删除?')){javascrtpt:window.location.href = 'DeleteOneNews?function=user_delete&destination=content_user.jsp&table=news&newsid=<%=tem.getNewsId()%>'}return false;}">删除a>
td>
tr>
<%
}
%>
接下来介绍一下Ajax实现的异步传输:
上面的JSP页面最后有三种button,每种button里面有一个onclick方法,该方法就是使用JavaScript实现的方法,然后使用Ajax技术将数据传送给后台的Servlet。
query.js是普通用户和管理员公用的所以里面还有一个checkpass方法用于管理员审核文章。其代码如下:
/**
* Created by 32706 on 2017/4/5.
*/
var xmlHttp=false;
function createXMLHttpRequest()
{
if (window.ActiveXObject) //在IE浏览器中创建XMLHttpRequest对象
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(ee){
xmlHttp=false;
}
}
}
else if (window.XMLHttpRequest) //在非IE浏览器中创建XMLHttpRequest对象
{
try{
xmlHttp = new XMLHttpRequest();
}
catch(e){
xmlHttp=false;
}
}
}
function queryInfo( id,table)
{
id=id.toString();
createXMLHttpRequest(); //调用创建XMLHttpRequest对象的方法
xmlHttp.onreadystatechange=callback; //设置回调函数
//向QueryOneNews这个Servlet查询从表table中的编号为id的文章的详细信息
var url="QueryOneNews?table="+table+"&newsid="+id;
alert("查询编号为:"+id+"的文章详情?");
xmlHttp.open("post",url,true); //向服务器端发送请求
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");
xmlHttp.send(null);
function callback()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var data= xmlHttp.responseText;
//分离每个字段的值,得到字段的属性值
var parameters=data.split("||");
var id=parameters[0]
var head=parameters[1];
var author=parameters[2];
var time=parameters[3];
var type=parameters[4]
var content=parameters[5];
//设置html中标签里的值
document.getElementById("check_id").value=id; document.getElementById("check_head").value=head; document.getElementById("check_author").value=author; document.getElementById("check_time").value=time; document.getElementById("check_type").value=type; document.getElementById("check_content").value=content;
}
}
}
}
function checkpass() {//审核通过
createXMLHttpRequest(); //调用创建XMLHttpRequest对象的方法
xmlHttp.onreadystatechange=callback; //设置回调函数
var newsid=document.getElementById("check_id").value;
var url="checkNews?newsid="+newsid;
xmlHttp.open("post",url,true); //向服务器端发送请求
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");
xmlHttp.send(null);
function callback()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
alert("审核通过,已完成");
location.reload();
}
}
}
这里做一下总结:
一、 前台JSP调用Servlet的三种方法(可能不完整):
1.在form标签里指定action字段,然后按钮type设置为submit,这样就能在点击按钮的时候自动提交form里的所有数据到Servlet。通常用在登录等表单的提交。
示例:
<form method="post" action="checkLogin_user">
2.在button等标签里的onclick属性里设置需要调用的Servlet,通常适用于简单的页面跳转或者数据提交。实际上也是调用的JavaScript的方法。
示例:
<a class="button border-red button-little" href="#" onclick="javascrtpt:window.location.href = 'DeleteOneNews?function=user_delete&destination=content_user.jsp&table=news&newsid=<%=tem.getNewsId()%>'">删除a>
3.使用JavaScript的函数来处理,典型的就是项目中需要用到的Ajax,使用JavaScript来获得页面中的标签的值然后传输给Servelt进行处理,并将结果显示在原先的页面上。
示例:
var xmlHttp=false;
function createXMLHttpRequest()
{
if (window.ActiveXObject) //在IE浏览器中创建XMLHttpRequest对象
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(ee){
xmlHttp=false;
}
}
}
else if (window.XMLHttpRequest) //在非IE浏览器中创建XMLHttpRequest对象
{
try{
xmlHttp = new XMLHttpRequest();
}
catch(e){
xmlHttp=false;
}
}
}
function queryInfo( id,table)
{
id=id.toString();
createXMLHttpRequest(); //调用创建XMLHttpRequest对象的方法
xmlHttp.onreadystatechange=callback; //设置回调函数
var url="QueryOneNews?table="+table+"&newsid="+id;
alert("查询编号为:"+id+"的文章详情?");
xmlHttp.open("post",url,true); //向服务器端发送请求
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");
xmlHttp.send(null);
function callback()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var data= xmlHttp.responseText;
}
}
}
}
4.直接使用“< a >”标签中的href属性指定所要访问的Servlet。不过该方法好像只能实现get方式。
示例:
<a href="Authorization_user?id=<%=tem.getId()%>" class="icon-file">文章授权a>
当数据传输给Servlet后,Servlet调用后台的方法就和普通的Java编程没有多大区别了,所以这里就不再赘述了。如有不足请多多包涵~~