Web程序设计
实验报告
院别计算机与通信工程学院专业计算机科学与技术
班级学号
姓名
成绩
2013 年12 月27
【实验编号】 1
【实验名称】熟悉环境和css的使用
【实验内容】
完成三个html页面,三个页面之间可以通过链接互相访问,自由的设置其中的控件,使用一个css表单来美化这三个页面。
【实验步骤】
index.html
1.html
2.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);
}
【实验编号】 2
【实验名称】熟悉DOM的原理和使用
【实验内容】
在刚才建立的index.html页面添加一个删除按钮,做一个基于DOM的js函数,每次点击删除按钮就会将刚才生成的一个控件删除。
【实验步骤】
代码:
var aa=0,bb=0,cc=0;
function add(){
var type=document.getElementById("list1");
if(type.selectedIndex=='0'){
var t=document.createElement("input");
t.setAttribute("type","text");
t.setAttribute("value","text");
t.setAttribute("id",aa);
aa=aa+1;
var base=document.getElementById("base");
base.appendChild(t);
var br=document.createElement("br");
base.appendChild(br);
}
if(type.selectedIndex=="1"){
var b=document.createElement("input");
b.setAttribute("type","button");
b.setAttribute("value","button");
b.setAttribute("id",bb);
bb=bb+1;
var base=document.getElementById("base");
base.appendChild(b);
var br=document.createElement("br");
base.appendChild(br);
}
if(type.selectedIndex=="2"){
var c=document.createElement("input");
c.setAttribute("type","Checkbox");
c.setAttribute("text","Checkbox");
c.setAttribute("id",cc);
cc=cc+1;
var base=document.getElementById("base");
base.appendChild(c);
var br=document.createElement("br");
base.appendChild(br);
}
}
function del() {
var type=document.getElementById("list1");
if(type.selectedIndex=='0'){
var t=document.getElementById(aa-1);
var base=document.getElementById("base");
base.removeChild(t);
aa=aa-1;
}
if(type.selectedIndex=="1"){
//alert(bb);
var m=document.getElementById(bb-1);
var base=document.getElementById("base");
base.removeChild(m);
bb=bb-1;
}
if(type.selectedIndex=="2"){
var n=document.getElementById(cc-1);
var base=document.getElementById("base");
base.removeChild(n);
cc=cc-1;
}
}
截图:
【实验编号】 3
【实验名称】学习使用servlet
【实验内容】将一个字符串使用doGet方法提交给Servlet,Servlet统计其中每个字母出现的次数,然后打印在页面上【实验步骤】
代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
This is my JSP page.
【实验编号】4
【实验名称】使用servlet和JDBC访问数据库
【实验内容】
制作一个页面,通过向数据库中添加、查找来实现用户注册,登录功能。【实验步骤】
源代码:
Index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServ erPort()+path+"/";
%>
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.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* 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.
*
* 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("");
out.println("");
out.println("
out.println("
");out.print(" password is correct ");
out.println(" ");
out.println("");
out.flush();
out.close();
}
}
/**
* Initialization of the servlet.
*
* @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.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The DO2Get method of the servlet.
*
* 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.
*
* 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("");
out.println("");
out.println("
out.println("
");out.print(" Login Successfully");
out.println(" ");
out.println("");
out.flush();
out.close();
}
/**
* Initialization of the servlet.
*
* @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();
}
}
}
Web程序设计实验报告 姓名:冯刚 学号:200905030324 班级:计科3班
Html代码: 1.首页代码