鐢↗SP+Javabean+Servlet瀹炵幇涓€涓姇绁ㄧ郴缁?/a>

时间:2022-01-24 19:06:18

//index   鐢ㄤ簬鐧婚檰绛夊姛鑳界殑瀹炵幇

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<head>
<!-- Copyright 2005 Macromedia, Inc. All rights reserved. -->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>娆㈣繋</title>
<link rel="stylesheet" href="3col_leftNav.css" type="text/css" />
<style type="text/css">
<!--
.STYLE1 {font-size: 18%}
.STYLE3 {
 font-size: large;
 font-weight: bold;
 color: #996600;
}
.STYLE4 {
 font-size: medium;
 font-weight: bold;
}
.STYLE5 {color: #00CC66}
-->
</style>
</head>
<body>
<form name="form1" method="post" action="login.jsp">
<hr>
 
  <div id="globalNav"> <a href="index.jsp">棣栭〉</a> |  | <a href="registration.jsp">娉ㄥ唽</a> | |<a href="./http://www.jmu.edu.cn"> 闆嗙編澶у</a> ||</div>
  <h2 id="pageName">闆嗙編澶у</h2>

  <div class="STYLE1" id="breadCrumb" >
    <p>娆㈣繋鐧婚檰鍒伴泦缇庡ぇ瀛︽姇绁ㄧ郴缁熻繘琛屾柊鏍″悕鐨勬姇绁紒鏂扮敤鎴疯娉ㄥ唽鍐嶇櫥闄嗭紝璋㈣阿锛?lt;/p>
    <hr>
  </div>
<div id="navBar">
  <div id="search">
   
      <label>鐢ㄦ埛鍚?lt;/label>
    <input name="userId" type="text" size="20" />
   
 
  </div>
  <div class="relatedLinks">
    <h3>瀵嗙爜 <br />
 <input name="password" type="password" size="20"/ >
 <input name="submit" type="submit" value="go" size="50" />
    </h3>
  </div>
 
  <div class="relatedLinks">
  
    <p>&nbsp;</p>
    <p class="STYLE4"><a href="registration.jsp" class="STYLE5">濡傛灉浣犳槸鏂扮敤鎴疯鐐瑰嚮杩欓噷娉ㄥ唽!</a></p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
  </div>
 
</div>
<!--end navBar div -->
<div id="headlines">
  <h3><%@ include file="CounterApp.jsp"%></h3>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p><a href="#">.</a> </p>
  <p>&nbsp;</p>
  <div id="advert"></div>
</div>
<!--end headlines -->
<div id="content">
  <div class="feature"> <img src="04.jpg" alt="" width="225" height="228" />
    <h3 class="STYLE4">闆嗙編澶у绠€浠?lt;/h3>
    <p>Jimei University, approved by the Ministry of Education, is a key   provincial multi-disciplined university which was combined into one from five   individual institutes and colleges in Jimei Schools Village, namely, Jimei   Navigation Institute, Xiamen Fisheries College, Fujian Physical Education   College, Jimei Finance and Economics Institute, and Jimei Teachers College in   October 1994. Secretary of CPC of JMU: Prof. Xiangzhong Zhang<br />
      President of JMU: Prof.   Jiande Gu<br />
      Add: Jimei Schools Village, Xiamen, 361021, Fujian Province, P. R.   China<br />
      Tel: 0086-592-6181097 Fax: 0086-592-6180120<br />
      
  </div>
  <div class="story">
    <h3>
      <!--end content -->
</h3>
  </div>
</div>
<div id="siteInfo"> <img src="20055291316679.jpg" width="65" height="21" /><span class="STYLE3">welcome</span> <span class="STYLE3">welcome</span> <span class="STYLE3">welcome</span> <span class="STYLE3">welcome</span> <span class="STYLE3">welcome</span> <span class="STYLE3">welcome</span> <span class="STYLE3">welcome</span> <span class="STYLE3">welcome</span> <span class="STYLE3">welcome</span></div>
<br />
</form>
</body>
</html>

//楠岃瘉鐧婚檰鐣岄潰

<%@ include file="include.inc"%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
//鑾峰緱璇锋眰鐨勫弬鏁般€?br>String id=request.getParameter("userId");
String psw=request.getParameter("password");
//杩炴帴鏁版嵁搴?br>Class.forName(CLASSFORNAME);
Connection con = DriverManager.getConnection(SERVANDDB,USER,PWD);
Statement statement=con.createStatement();
String isCorrect="select * from user_info where userId='" + id + "' and password='" + psw + "'";
ResultSet result=statement.executeQuery(isCorrect);
 session.setAttribute("isLog",new String("0"));

if(!result.next())
{
 
response.sendRedirect("index.jsp");//璐﹀彿鎴栧瘑鐮侀敊璇紝杞叆閿欒椤甸潰
result.close();
statement.close();
con.close();
}
 //濡傛灉鏌ヨ鍚庢湁璁板綍锛岃鏄庣櫥褰曢獙璇佹垚鍔熴€?br>else
{
      //淇濆瓨鐧诲綍淇℃伅鍒皊ession瀵硅薄涓€?br>   session.setAttribute("name",result.getString("name"));
   session.setAttribute("id",result.getString("userId")); 
   session.setAttribute("email",result.getString("email")); 
   session.setAttribute("fav",result.getString("fanvcategory")); 
   session.setAttribute("userType",new Integer(result.getInt("userType")));
   session.setAttribute("re","0");  
   System.out.println(session.getAttribute("userType"));
   session.setAttribute("isLog",new String("1"));
   int count=result.getInt("userLogCount");
   session.setAttribute("userLogCount",new Integer(count)); 
   count++;
   session.setAttribute("userLastLogTime",result.getString("userLastLogTime")); 
   java.util.Date time1=new java.util.Date();//鑾峰緱绯荤粺褰撳墠鏃堕棿
      String sqltime=new Timestamp(time1.getTime()).toString();//灏嗗綋鍓嶆椂闂磋浆鎹㈡垚sql server鍙互璇嗗埆鐨勫璞?nbsp;    
  //鏇存柊鐧诲綍淇℃伅銆?br>   statement.execute("update user_info set userLogCount="+count+",userLastLogTime='"+sqltime+"' where userId='" + id +"'");
   //鍏抽棴鏁版嵁搴撹繛鎺ョ浉鍏崇殑璧勬簮銆?br>  statement.close();
  con.close();  
  //鎶婇〉闈㈡淳鍙戝埌鐩殑
  response.sendRedirect("tpiao.jsp"); 
}
%>

//鎶曠エ鐣岄潰

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>">
<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">
   
<!-- Copyright 2005 Macromedia, Inc. All rights reserved. -->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>娆㈣繋</title>
<link rel="stylesheet" href="3col_leftNav.css" type="text/css" />
<style type="text/css">
<!--
.STYLE1 {font-size: 18%}
.STYLE2 {color: #006699}
.STYLE5 {font-size: small}
.STYLE20 {font-size: 10px}
-->
</style>
</head>
<body>
 
<hr>
 
<div id="globalNav"> <a href="index.jsp">棣栭〉</a> |  | <a href="logout.jsp">娉ㄩ攢</a> | |<a href="./http://www.jmu.edu.cn"> 闆嗙編澶у</a> ||    </div>
  <h2 id="pageName">闆嗙編澶у</h2>

<div class="STYLE1" id="breadCrumb" >
    <p><p align="center">HI锛?lt;%=session.getAttribute("name")%>,鎮ㄦ槸绗?lt;%=session.getAttribute("userLogCount")%>娆$櫥褰曪紝鎮ㄤ笂娆$櫥褰曠殑鏃堕棿鏄細<%=session.getAttribute("userLastLogTime")%> 锛佽閫夋嫨鍙互鎸叴鎴戞牎鐨勪竴涓柊鏍″悕瀛楋紒</p>
    <hr>
</div>
  <div id="navBar">
  <div id="search"></div>
  <div class="relatedLinks">
  <FORM name="addInfo" action="AddInfo" method=GET>
    <table width="121" height="343" border="0">
      <tr>
        <td width="37"><label>
          <input type="radio" name="Nname" value="a" />
        </label></td>
        <td width="68"><span class="STYLE5">涓崕鍎垮コ澶у</span></td>
      </tr>
      <tr>
        <td><label>
          <input type="radio" name="Nname" value="b" />
        </label></td>
        <td><span class="STYLE5">鍘﹂棬绉戞妧澶у</span></td>
      </tr>
      <tr>
        <td><input type="radio" name="Nname" value="c" /></td>
        <td><span class="STYLE5">鍋囨棩甯濆浗澶у</span></td>
      </tr>
      <tr>
        <td><input type="radio" name="Nname" value="d" /></td>
        <td><span class="STYLE5">涓浗鍢夊簹澶у</span></td>
      </tr>
      <tr>
        <td height="23" colspan="2"><label>
          <INPUT type="Submit" name="button2" value="鎻愪氦 method=get">
        </label></td>
      </tr>
    </table>
 </form>
  </div>
 
</div>
<!--end navBar div -->
<div id="headlines"><div class="relatedLinks">
  <FORM name="addInfo" action="AddInfo" method=post>
    <table width="121" height="343" border="0">
      <tr>
        <td width="37"><label>
          <input type="radio" name="Nname" value="a" />
        </label></td>
        <td width="68"><span class="STYLE5">涓崕鍎垮コ澶у</span></td>
      </tr>
      <tr>
        <td><label>
          <input type="radio" name="Nname" value="b" />
        </label></td>
        <td><span class="STYLE5">鍘﹂棬绉戞妧澶у</span></td>
      </tr>
      <tr>
        <td><input type="radio" name="Nname" value="c" /></td>
        <td><span class="STYLE5">鍋囨棩甯濆浗澶у</span></td>
      </tr>
      <tr>
        <td><input type="radio" name="Nname" value="d" /></td>
        <td><span class="STYLE5">涓浗鍢夊簹澶у</span></td>
      </tr>
      <tr>
        <td height="23" colspan="2">          <label>
           <INPUT type="Submit" name="button2" value="鎻愪氦method=post">
        </label>        </td>
      </tr>
    </table>
 </form>
  </div></div>
<!--end headlines -->
<div id="content">
  <div class="feature"> <img src="04.jpg" alt="" width="225" height="228" />
    <h3>闆嗙編澶у绠€浠?lt;/h3>
    <p>Jimei University, approved by the Ministry of Education, is a key   provincial multi-disciplined university which was combined into one from five   individual institutes and colleges in Jimei Schools Village, namely, Jimei   Navigation Institute, Xiamen Fisheries College, Fujian Physical Education   College, Jimei Finance and Economics Institute, and Jimei Teachers College in   October 1994. Secretary of CPC of JMU: Prof. Xiangzhong Zhang<br />
President of JMU: Prof.   Jiande Gu<br />
Add: Jimei Schools Village, Xiamen, 361021, Fujian Province, P. R.   China<br />
Tel: 0086-592-6181097 Fax: 0086-592-6180120<br />
Website:   www.jmu.edu.cn</p>
  </div>
  <div class="story">
    <h3>
      <!--end content -->
</h3>
  </div>
</div>
<div id="siteInfo"> <img src="20055291316679.jpg" width="65" height="21" /> <span class="STYLE2">  Copyright @ 2006 wen31.鏂? All rights reserved</span></div>
<br />

</body>
</html>
//娉ㄩ攢鐣岄潰

<%@ page language="java" contentType="text/html; charset=gb2312"%>
<HTML><HEAD><TITLE>CyberCreations</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2600.0" name=GENERATOR>
<META content=FrontPage.Editor.Document name=ProgId>
<META content=b name="Microsoft Border">
<link href="Forum_admin.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY bgcolor="#FFFFFF">
<center>
<%
session.invalidate();
response.sendRedirect("index.jsp");
%>
<br>
<br>
姝e湪娉ㄩ攢.......
</center>
</body>
</html>
//涓轰簡闃叉涓€涓敤鎴风殑澶氭鎶曠エ 鍚庡嚭鐜扮殑寮鸿閫€鍑虹晫闈€傚叿浣撳疄鐜版柟娉曞湪鍚庨潰


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>error.html</title>
    <META HTTP-EQUIV="REFRESH" CONTENT="1; URL=index.jsp">  
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
   
  </head>
 
  <body>
     <script language="LiveScript">
    alert("浣犱互鎻愪氦杩?);
  </script>
 
  </body>
</html>
//Addinfo.java

package org.mylisky.servelet;

import java.beans.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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


public class AddInfo extends HttpServlet {

    /**
     * Constructor of the object.
     */
    private Connection con;
    public void init() throws ServletException {
        String url = getInitParameter("url");
        String DBServer = getInitParameter("DBServer");
        String UserName = getInitParameter("UserName");
        String PassWord = getInitParameter("PassWord");
        System.out.println("url=" + url);

        try {
            Class.forName(url);
            con = DriverManager.getConnection(DBServer, UserName, PassWord);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
    private String trans(String chi){
        String result = null;
        byte temp[];
        try{
            temp=chi.getBytes("iso-8859-1");
            result=new String(temp,"GB2312");
        }
        catch(UnsupportedEncodingException e){e.printStackTrace();}
        return result;
    }

  
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            request.setCharacterEncoding("GB2312");
       
       
        String Nname = (String)request.getParameter("Nname");
        String r = (String)request.getSession().getAttribute("re");
        if(r.equals("1")){response.sendRedirect("reError.html");}
       
        else{try{
            java.sql.Statement t = con.createStatement();
            ResultSet nt = t.executeQuery("select * from advice ");
            nt.next();
            int i = nt.getInt(Nname);
            int ni = i+1;
            java.sql.Statement s = con.createStatement();
            s.executeUpdate("update advice set "+Nname+"='"+ni+"'");
           
          }catch(SQLException e){e.printStackTrace();}
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/viewInfo");
        requestDispatcher.forward(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 {
           doGet(request,response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occure
     */
   

}
//viewInfo.java

package org.mylisky.servelet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class viewInfo extends HttpServlet {

    Connection con;

    public void init() throws ServletException {
        String url = getInitParameter("url");
        String DBServer = getInitParameter("DBServer");
        String UserName = getInitParameter("UserName");
        String PassWord = getInitParameter("PassWord");
        System.out.println("url=" + url);

        try {
            Class.forName(url);
            con = DriverManager.getConnection(DBServer, UserName, PassWord);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * 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 {
        response.setContentType("image/jpeg");
        String r = (String) request.getSession().getAttribute("re");
        System.out.print(r);
        if (r.equals("1")) {
            response.sendRedirect("reError.html");
        }
        request.getSession().setAttribute("re", "1");
        ServletOutputStream out = response.getOutputStream();
        BufferedImage image = new BufferedImage(800, 600,
                BufferedImage.TYPE_INT_RGB);
        int a = 0, b = 0, c = 0, d = 0, total = 0;
        final int size = 500;
        final int high = 50;
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from advice");
            if (rs.next()) {
                a = rs.getInt("a");
                b = rs.getInt("b");
                c = rs.getInt("c");
                d = rs.getInt("d");
                total = a + b + c + d;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, 800, 600);
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font("鍗庢枃褰╀簯", Font.BOLD, 50));
        graphics.drawString("鎶曠エ缁撴灉", 300, 40);

        graphics.setFont(new Font("闅朵功", Font.PLAIN, 20));
        graphics.drawLine(120, 100, 120, 470);
        graphics.drawLine(120, 470, 720, 470);
        graphics.drawString("鎶曠エ浜烘暟", 0, 140);
        graphics.drawString("绂忓缓绗竴澶у", 0, 210);
        graphics.drawString("鍘﹂棬绉戞妧澶у", 0, 280);
        graphics.drawString("鍘﹂棬浜ら€氬ぇ瀛?, 0, 350);
        graphics.drawString("闆嗙編鍢夊簹澶у", 0, 420);

        graphics.fillRect(120, 120, size, high);
        graphics.drawString(String.valueOf(total), 630, 140);

        graphics.fillRect(120, 190, size * a / total, high);
        graphics.drawString(String.valueOf(a), (size * a / total) + 140, 210);

        graphics.fillRect(120, 260, size * b / total, high);
        graphics.drawString(String.valueOf(b), (size * b / total) + 140, 280);

        graphics.fillRect(120, 330, size * c / total, high);
        graphics.drawString(String.valueOf(c), (size * c / total) + 140, 350);

        graphics.fillRect(120, 400, size * d / total, high);
        graphics.drawString(String.valueOf(d), (size * d / total) + 140, 420);

        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(image);
        out.close();

    }

    /**
     * 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 {

        doGet(request, response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException
     *             if an error occure
     */

}
//xml 鐨勯厤缃?/p>

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <servlet>
  <description>This is the description of my J2EE component</description>
  <display-name>This is the display name of my J2EE component</display-name>
  <servlet-name>AddInfo</servlet-name>
  <servlet-class>org.mylisky.servelet.AddInfo</servlet-class>
  <init-param>
   <param-name>url</param-name>
   <param-value>com.microsoft.jdbc.sqlserver.SQLServerDriver</param-value>
  </init-param>
  <init-param>
   <param-name>DBServer</param-name>
   <param-value>jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jspdev</param-value>
  </init-param>
  <init-param>
   <param-name>UserName</param-name>
   <param-value>sa</param-value>
  </init-param>
  <init-param>
   <param-name>PassWord</param-name>
   <param-value>123</param-value>
  </init-param>
 </servlet>
 <servlet>
  <description>This is the description of my J2EE component</description>
  <display-name>This is the display name of my J2EE component</display-name>
  <servlet-name>viewInfo</servlet-name>
  <servlet-class>org.mylisky.servelet.viewInfo</servlet-class>
  <init-param>
   <param-name>url</param-name>
   <param-value>com.microsoft.jdbc.sqlserver.SQLServerDriver</param-value>
  </init-param>
  <init-param>
   <param-name>DBServer</param-name>
   <param-value>jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jspdev</param-value>
  </init-param>
  <init-param>
   <param-name>UserName</param-name>
   <param-value>sa</param-value>
  </init-param>
  <init-param>
   <param-name>PassWord</param-name>
   <param-value>123</param-value>
  </init-param>
 </servlet>


 <servlet-mapping>
  <servlet-name>AddInfo</servlet-name>
  <url-pattern>/AddInfo</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>viewInfo</servlet-name>
  <url-pattern>/viewInfo</url-pattern>
 </servlet-mapping>

</web-app>