zoukankan      html  css  js  c++  java
  • 保存学生上课信息(软工概论)

     

    程序设计思想:

    看到题目,首先想到该用哪些功能可以实现这些程序。Jsp文件中要建立相关的表格,题中的保存,就是代码中的提交,用户从网页中输入参数之后,severlat中的java程序要获取从网页中传过来的参数,并将其转化为可以进行中文识别的编码,utf-8.severlat中的java代码获取到网页传过来的参数后,要插入到数据库中,这时,我们需要建立一个链接数据库的类,并建立一个对象,链接数据库。 对于题目要求的判断老师和教室开头,获取参数后,我们就要判断,如果符合题目的条件,才能写入数据库中,如果不符合,就要用reponse向网页中返回提示信息。这里又涉及到了编码问题,如果编码问题不能解决,返回到网页中的提示信息不能是汉字。在判断老师的时候,乱码问题困扰了我好久,自己一再在百度搜索,<%request.setCharacterEncoding("utf-8");%> 百度到的这个代码可以解决后台从前台获取数据中文乱码的问题,这是解决判断老师问题的第一步,然后就是从后台向前台网页反馈信息了,如果用户输入的老师不是题目要的老师,我们需要反馈到前台,我在这里又出现了乱码问题,不能通过消息框向前台返回中文信息,我通过百度,找到了/* response.setHeader("Content-type", "text/html;charset=UTF-8");  

    这句话的意思,是告诉servletUTF-8转码,而不是用默认的ISO8859  

    response.setCharacterEncoding("UTF-8");  

    String data = "中国";  

    PrintWriter pw = response.getWriter();  

    pw.write(data);*/

    这几行代码,通过分析,我解决了反馈信息这个问题。

    源代码:  

      Severlat :

       package severlat;

    import java.io.IOException;

    import java.io.PrintWriter;

    import javax.servlet.ServletException;

    import javax.servlet.annotation.WebServlet;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    /**

     * Servlet implementation class add

     */

    @WebServlet("/add")

    public class add extends HttpServlet {

    private static final long serialVersionUID = 1L;

           

        /**

         * @see HttpServlet#HttpServlet()

         */

        public add() {

            super();

            // TODO Auto-generated constructor stub

        }

    /**

     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

     */

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

    // TODO Auto-generated method stub

    response.getWriter().append("Served at: ").append(request.getContextPath());

    }

    /**

     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

     */

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

    // TODO Auto-generated method stub

    request.setCharacterEncoding("utf-8");

    response.setHeader("Content-type", "text/html;charset=UTF-8");

    response.setCharacterEncoding("UTF-8");

    PrintWriter out = response.getWriter();

    String  username = request.getParameter("username");

    String  homeaddress=request.getParameter("address");

    if(!homeaddress.equals("王建民"))

    {

    out.print("<script language='javascript'>alert('你只能选择王建民老师的课');window.location.href='add.jsp';</script>");

    }

    String Pname =request.getParameter("Pname");

    String miaoshu=request.getParameter("miaoshu");

    /* response.setHeader("Content-type", "text/html;charset=UTF-8");  

    这句话的意思,是告诉servletUTF-8转码,而不是用默认的ISO8859  

    response.setCharacterEncoding("UTF-8");  

    String data = "中国";  

    PrintWriter pw = response.getWriter();  

    pw.write(data);*/

    if(homeaddress.equals("王建民")) {

         String s = "insert into biao(username,homeaddress,Pname,miaoshu) values ('"+username+"','"+homeaddress+"','"+Pname+"','"+miaoshu+"')";

    DB db= new DB();

    int i = db.executeUpdate(s);

    if(!homeaddress.equals("王建民"))

    {

    out.print("不能这样");

    }

    if(i==1)

    {

    out.print("<script language='javascript'>alert('添加 sucess,please click confirm to need add UI');window.location.href='add.jsp';</script>");

    //脚本语言

    }

    else

    {

    out.println("<script language='javaScript'> alert('Add fail,please input again!');</script>");

    }

    }

    } }

    链接数据库类:

    package severlat;

    import java.sql.*;

    public class DB {

    String userName = "sa";

        String userPwd = "yyh678";

       String connStr  = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=XUQIU";

         String driverStr=  "com.microsoft.sqlserver.jdbc.SQLServerDriver";

       private Connection conn=null;

       private Statement stmt=null;

        //Statement 可以执行数据库查询  更新语句

    public DB()

    {

    try{

    Class.forName(driverStr);

    conn = DriverManager.getConnection(connStr,

    userName, userPwd);

    //conn是 数据库链接地址

    stmt = conn.createStatement();

    }

    catch(Exception ex){System.out.println("数据库连接失败");}

    }

    public int executeUpdate(String s)

    {

    int result=0;

    try{result=stmt.executeUpdate(s);

    //更新固定返回01

    }

    catch(Exception ex){System.out.println("更新失败"+ex.getMessage());}

    return result;

    }

    public ResultSet executeQuery(ResultSet rs, String s)

    {

    try{rs=stmt.executeQuery(s);}

    catch(Exception ex){System.out.println("查询失败"+ex);}

    return rs;

    }

    public void close()

    {

    try{

    stmt.close();

    conn.close();

    }

    catch(Exception e){}

    }

    }

    Jsp文件:

    <%@ page language="java" contentType="text/html; charset=utf-8"

        pageEncoding="utf-8"%>

    <%@page import = "java.sql.*" %>

    <%request.setCharacterEncoding("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="Content-Type" content="text/html; charset=utf-8">

    <title>Insert title here</title>

    </head>

    <body>

    <form name = "form1" method = "post" action = "add" onsubmit = "return isValidate(form1)" >

    <table>

    <tr> <td>课程名称</td> <td><input type ="text" name="username"></td></tr>

    <tr> <td>上课教师</td>  <td><input type ="text" name="address"></td></tr>

    <tr> <td>上课地点</td>  <td><input type ="text" name="Pname"></td></tr>

    <tr><td colspan = "2"><input type = "submit" name = "submit" value = "提交" style = "100px;height:50px;font-size:20px;border:1;"></td>

     </tr>

    </table>

    </form>

    </body>

    </html>

    验证截图:  

  • 相关阅读:
    简单排序(冒泡、选择、插入)
    配置Tomcat数据源
    使用spring的邮件发送功能
    安装Tomcat
    Spring-MongoDB简单操作
    cisco ASA ios升级或恢复
    ASA 用TFTP 备份配置方法
    DELL MD3200i存储控制器解锁方法
    IBM ServerGuide引导盘全系列下载网址
    ASA5520远程配置 telnet,ssh
  • 原文地址:https://www.cnblogs.com/yyh-678/p/7910942.html
Copyright © 2011-2022 走看看