web程序设计报告

                                                  

                                                         

Web程序设计

实验报告

2013  年 12  月 27  

2.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>2.html</title>

   

    <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="mycss.css">

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

 

  <body>

    <h1> This is my HTML page2. <br> </h1>

  </body>

</html>

Mycss.css

@CHARSET "UTF-8";

h1{color: red;

background-color: gray;

  text-align: left;

  font-size: 58pt;}

body{color: blue;

background-image: url(Sunset.jpg);

}


源代码:

Index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

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%>">

   

    <title>My JSP 'index.jsp' starting page</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>

 

  <form method="get" action="servlet/QueryServlet2">

  <fieldset>

  <legend align="center"> JDBC注册 </legend>

  <center>

  <p> 用户名: <input type="text" size="20" name="user" > </p>

 

  <p> 密&nbsp;&nbsp;&nbsp;码:  &nbsp;<input type="text" size="20" name="password"> </p>

<p>

<input type="submit" value="注册" name="button2" >

</p>

</center>

</fieldset>

</form>

<form method="get" action="servlet/QueryServlet">

  <fieldset>

  <legend align="center"> JDBC登录 </legend>

  <center>

  <p> 用户名: <input type="text" size="20" name="user1" > </p>

 

  <p> 密&nbsp;&nbsp;&nbsp;码:  &nbsp;<input type="text" size="20" name="password1"> </p>

<p>

<input type="submit" value="登录" name="button1"> &nbsp; &nbsp; &nbsp;

</p>

</center>

</fieldset>

</form>

  </body>

</html>

QueryServlet.java

package neuq.dzxx;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class QueryServlet extends HttpServlet {

       /**

        * Constructor of the object.

        */

       private DataOperator DO;

      

       public QueryServlet() {

              super();

       }

       /**

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

              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 {

             

             

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

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

              String price="";

              //String price = "no price";

              try {

                     ResultSet rs = DO.query("select password from Info where name='"+user1+"';");

                     if(rs.next()){

                      price = rs.getString("password");

                     }

              } catch (SQLException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

     if(price==password1) {

              response.setContentType("text/html");

              PrintWriter out = response.getWriter();

              out

                            .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

              out.println("<HTML>");

              out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");

              out.println("  <BODY>");

              out.print("    password is correct ");

              out.println("  </BODY>");

              out.println("</HTML>");

              out.flush();

              out.close();

     }

       }

       /**

        * Initialization of the servlet. <br>

        *

        * @throws ServletException if an error occure

        */

       public void init() throws ServletException {

             

       }

}

QueryServlet2.java

package neuq.dzxx;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class QueryServlet2 extends HttpServlet {

       /**

        * Constructor of the object.

        */

       private DataOperator DO2;

       public QueryServlet2() {

              super();

       }

       /**

        * Destruction of the servlet. <br>

        */

       public void destroy() {

              super.destroy(); // Just puts "destroy" string in log

              // Put your code here

       }

       /**

        * The DO2Get 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 DO2Post 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 {

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

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

              //String price = "no price";

              try {

                     DO2.insert("insert into Info values('"+user+"','"+password+"')");

              } catch (SQLException e) {

                     // TODO2 Auto-generated catch block

                     e.printStackTrace();

              }

              response.setContentType("text/html");

              PrintWriter out = response.getWriter();

              out

                            .println("<!DO2CTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

              out.println("<HTML>");

              out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");

              out.println("  <BODY>");

              out.print("    Login  Successfully");

              out.println("  </BODY>");

              out.println("</HTML>");

              out.flush();

              out.close();

       }

       /**

        * Initialization of the servlet. <br>

        *

        * @throws ServletException if an error occure

        */

       public void init() throws ServletException {

              DO2 = DataOperator.getInstance();

              try {

                     String creatTableInfo = "create table Info (name int not null, " +

                     "password int not null " +

                     ");";

                     DO2.creatTable(creatTableInfo);

                     DO2.insert("insert into Info values('1','10')");

                    

              } catch (SQLException e) {

                     // TODO2 Auto-generated catch block

                     e.printStackTrace();

              }

       }

}

相关推荐