前言:
JSP 包含了HTML,Java Script, CSS/DIV等, 学习JSP之前起码保证对HTML有个了解。
JSP本质还是Servlet, 不过用Servlet写一堆的out.println来输出界面太麻烦了, 所以出现了JSP,专注于界面方面, Servlet只需要关注数据/业务处理就好。
sample1:
在MyApp项目目录下创建一个index.jsp, 只写下面三行代码:
<%
response.sendRedirect("reguser.jsp");
%>
在浏览器输入地址访问:
http://localhost:8080/MyApp/index.jsp
接着创建reguser.jsp:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!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=GBK">
<title>输入用户名</title>
</head>
<body>
<br>
<form name="form" action="weluser.jsp" method="get">
<table width="200" border="1" align="center">
<tr>
<td> 用户名:</td>
<td> <input type="text" name="username" size="10"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交"> | <input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
接着创建weluser.jsp:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>欢迎您</title>
</head>
<body>
<center>
<%
String username = request.getParameter("username");
out.println("Welcome "+username+"!");
//获取用户名信息
//输出欢迎该用户的提示信息
%>
</center>
</body>
</html>
重新浏览器输入地址访问:
http://localhost:8080/MyApp/index.jsp
即可看到效果