servlet的应用及其他

一个jsp网页要传递一个数据给另一个网页或者要进行一些数据的录入操作,这是就用到servlet了,简单来说就是两个方法:doPost和doGet,参数传递进来,进行一系列操作,比如录入数据什么的,这就是servlet的作用。附代码:

package cn.edu.hit;

import java.io.IOException;
import java.io.PrintWriter;

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

public class HelloServlet extends HttpServlet {

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  this.doPost(request, response);
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to
  * post.
  *
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html;charset=gb2312");
  String operation = request.getParameter("operation");
  PrintWriter out = response.getWriter();
  DBConn db = new DBConn();
  if (operation.equals("input")) {
   String username = request.getParameter("username");
   String password = request.getParameter("password");
   String sex = request.getParameter("sex");
   String[] love = request.getParameterValues("love");
   String major = request.getParameter("major");
   String infor = request.getParameter("infor");
   infor = MyTools.toGB(infor);

   String sql = "insert into student(username,password,sex,love,major,infor) values('"
     + username
     + "','"
     + password
     + "','"
     + sex
     + "','"
     + love[0] + "','" + major + "','" + infor + "')";
   db.executeUpdate(sql);

   response.sendRedirect("student.jsp");
  } else if (operation.equals("delete")) {
   String username = request.getParameter("username");
   String sql = "delete from student where username = '" + username
     + "'";
   db.executeUpdate(sql);
   response.sendRedirect("student.jsp");
  } else if (operation.equals("modify")) {
   String username = request.getParameter("username");
   String password = request.getParameter("password");
   String sex = request.getParameter("sex");
   String[] love = request.getParameterValues("love");
   String major = request.getParameter("major");
   String infor = request.getParameter("infor");
   infor = MyTools.toGB(infor);

   String sql = "update student set password = '" + password
     + "',sex = '" + sex + "',love = '" + love[0]
     + "',major = '" + major + "',infor = '" + infor + "'where username = '"+username+"'";
   db.executeUpdate(sql);

   response.sendRedirect("student.jsp");
  }
  db.close();
 }
}

 


DBConn.java代码:

/就是一个操作数据库的类

package cn.edu.hit;

import java.sql.*;

public class DBConn {
 private Connection con;
 private Statement stmt;
 public DBConn() {
  try {
   Class.forName("com.mysql.jdbc.Driver");
   con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hit","root","root");
   stmt = con.createStatement();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public ResultSet executeQuery(String sql) {
  try {
   ResultSet rs = stmt.executeQuery(sql);
   return rs;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return null;
  }
 }
 public void executeUpdate(String sql) {
  try {
   stmt.executeUpdate(sql);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public void close() {
  try {
   stmt.close();
   con.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

student.jsp:

//主页:

<%@ page language="java" import="java.sql.*,cn.edu.hit.*" pageEncoding="GB2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
 <link rel="stylesheet" href="mystyle.css" type="text/css"/>
    <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="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
   <center><a href="input.html">增加学生</a></center>
    <table border="1" align="center">
    <tr>
    <td>用户名</td><td>密码</td><td>性别</td><td>爱好</td><td>专业</td><td>简介</td><td>删除</td><td>修改</td>
    </tr>
    <%
    DBConn db = new DBConn();
    ResultSet rs = db.executeQuery("select username,password,sex,love,major,infor from student");
    String username = null;
    String password = null;
    String sex = null;
    String love = null;
    String major = null;
    String infor = null;
    while(rs.next())
    {
     username = rs.getString(1);
     password = rs.getString(2);
     sex = rs.getString(3);
     love = rs.getString(4);
     major = rs.getString(5);
     infor = rs.getString(6);
    %>
    <tr>
    <td><%=username %></td><td><%=password %></td><td><%=sex %></td><td><%=love %></td><td><%=major %></td><td><%=infor %></td>
    <td><a href="HelloServlet?operation=delete&username=<%=username %>" οnclick="return confirm('确定删除吗?')">删除</a></td>
    <td><a href="modify.jsp?username=<%=username %>">修改</a></td>
    </tr>
    <%
    }
    db.close();
    %>
    </table>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值