实验要求:
①JSP版Helloworld 简单输出:Helloworld即可 ②Servlet版Helloworld 简单输出:Helloworld即可 ③连接数据库测试 参考:DBConn.jsp ④用循环连续输出三个你好,字体从小变大 JSP版与Servlet版都要完成
JSP版Helloworld
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello,world!</h1>
</body>
</html>
Servlet版Helloworld
package com.bnuz;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloWorld() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("<h1>HelloWorld</h1>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
连接数据库测试
<%@ page language="java" contentType="text/html; charset=GB2312" pageEncoding="GB2312"%>
<%@ page import="java.sql.*"%>
<!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=GB2312">
<title>Database Connection Test</title>
</head>
<body>
<%
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
String url = "jdbc:sqlserver://localhost:1433;databaseName=J2EE;integratedSecurity=true;";
String dbUser = "sa";
String dbPassword = "4580796";
Connection conn= DriverManager.getConnection(url,dbUser,dbPassword);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from T_Student";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
int id = rs.getInt("id");
String name = rs.getString("name");
String school = rs.getString("school");
out.println("Id="+ id + " Name="+ name + " School="+ school + "<BR/>");
}
conn.close();
}catch(ClassNotFoundException ex)
{
out.println("加载类失败:"+ex.getMessage());
}catch(SQLException ex2)
{
out.println("执行SQL出错:"+ ex2.getMessage());
}
%>
</body>
</html>
根据以上代码建立相关数据库及表。 如果出现加载类失败的问题,可能是由于连接数据库的jar包没有导入tomcat安装目录下的lib文件夹。或者该项目没有引入连接数据库的jar包。
用循环连续输出三个你好,字体从小变大
JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% for(int i = 3; i >=1; i--)
{
out.print("<h"+i+">"+"你好"+"</h"+i+">");
}
%>
</body>
</html>
Servlet版:
package com.bnuz;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloWorld() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
for(int i = 3; i >=1; i--) {
response.getWriter().append("<h"+i+">"+"HelloWorld"+"</h"+i+">");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
(。??)ノ菜鸟一枚,如有问题欢迎指出
|