目录
JSP入门
第1关:搭建你的第一个Web服务器
第3关:JSP基础测试题(一)
第4关:JSP基础(二)
第5关:JSP基础测试题(二)
基于MVC模式的用户登录
第1关:编写用户登录页面
第2关:登录验证
JSP基础之网站
第1关:显示所有用户列表
第2关:显示具体用户信息
第3关:添加用户
第4关:删除指定用户
JSP入门
第1关:搭建你的第一个Web服务器
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- 请在此 添加代码 -->
<!-- begin -->
hello educoder
<!-- end -->
</body>
</html>
第2关:JSP基础(一)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 请在此处添加代码 -->
<!-- begin -->
for(int i=1;i<10;i++){
for(int j=1;j<=i;j++){
System.out.print(j+"*"+i=i*j+" ");
}
System.out.println();
}
<!-- end -->
</body>
</html>
第3关:JSP基础测试题(一)
第4关:JSP基础(二)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>JSP脚本元素测试</title>
</head>
<body>
<!-- 创建一个公有的整形全局变量count 初始值为0-->
<!-- start -->
<%! int count=0; %>
<!-- end -->
<!-- 使用JSP脚本程序将count变量+1之后输出 -->
<!-- start -->
<%=count+1 %>
<!-- end -->
<!-- 使用JSP表达式将count的值输出 -->
<!-- start -->
使用表达式输出的count值为:
<%=count%>
<!-- end -->
<table width="800" cellpadding="0" border = 1>
<tr><td>i</td><td>i的平方</sup></td></tr>
<!-- 在这里使用JSP脚本程序输出表格的行和列,循环的变量请使用 "i" 效果图请看编程要求 -->
<!-- start -->
<%
for(int i=0;i<5;i++){
out.println(<table> </table>);
}
%>
<!-- end -->
</table>
</body>
</html>
第5关:JSP基础测试题(二)
基于MVC模式的用户登录
第1关:编写用户登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="login">
<p>username:<input name="userName"></p>
<p>password:<input name="password" type="password"></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
第2关:登录验证
注意:这里的异常只能用try-catch throws向上抛出代码检测不到
package chapter9;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/********* Begin *********/
Connection conn ;
PreparedStatement st ;
ResultSet rs=null;
String userName = req.getParameter("userName");
String password = req.getParameter("password");
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/university?user=root&password=123123";
conn=DriverManager.getConnection(url);
String sql = "select * from student" + " where USER_NAME = ? and PASSWORD = ?";
//预编译
st= conn.prepareStatement(sql);
st.setString(1,userName);
st.setString(2,password);
rs = st.executeQuery();
}catch (Exception e){
e.printStackTrace();
}
StudentBean studentbean=new StudentBean();
studentbean.setUserName(userName);
studentbean.setPassword(password);
HttpSession session = req.getSession();
try {
if (rs.next()){
studentbean.setStudentId(rs.getInt(1));
studentbean.setSex(rs.getString(4));
studentbean.setAge(rs.getInt(5));
studentbean.setDept(rs.getString(6));
session.setAttribute("account",studentbean);
resp.sendRedirect("success.jsp");
}else{
session.setAttribute("account",studentbean);
resp.sendRedirect("fail.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
/********* End *********/
}
}
JSP基础之网站
第1关:显示所有用户列表
package com.yotam.servlet;
import com.yotam.bean.User;
import com.yotam.dao.UserDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
@WebServlet("/users")
public class AllUsersServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*********Begin*********/
UserDao user=new UserDao();
req.setAttribute("model", user.getAllUsers());
req.getRequestDispatcher("users.jsp").forward(req, resp);
/*********End*********/
}
}
第2关:显示具体用户信息
package com.yotam.servlet;
import com.yotam.bean.User;
import com.yotam.dao.UserDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
// This servlet must be called: /showuser?id=<username>
@WebServlet("/showuser")
public class ShowUserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*********Begin*********/
//获取参数名为id的值
String user = req.getParameter("id");
UserDao userdao = new UserDao();
req.setAttribute("model", userdao.getUser(user));
req.getRequestDispatcher("showuser.jsp").forward(req, resp);
/*********End*********/
}
}
第3关:添加用户
package com.yotam.servlet;
import com.yotam.bean.User;
import com.yotam.dao.UserDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@WebServlet("/adduser")
public class AddUserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*********Begin*********/
String name=req.getParameter("name");
String phone=req.getParameter("phone");
String str=req.getParameter("friends");
List friends = Arrays.asList(str.split(","));
User user = new User(name,phone);
user.setFriends(friends);
UserDao userdao= new UserDao();
userdao.addUser(user);
req.setAttribute("model", user);
req.getRequestDispatcher("showuser.jsp").forward(req,resp);
/*********End*********/
}
}
第4关:删除指定用户
package com.yotam.servlet;
import com.yotam.bean.User;
import com.yotam.dao.UserDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
@WebServlet("/deluser")
public class DelUserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*********Begin*********/
String name = req.getParameter("name");
UserDao userDao = new UserDao();
Boolean isDeleted = userDao.delUser(name);
if(!isDeleted){
resp.setStatus(404);
req.getRequestDispatcher("updatefail.jsp").forward(req,resp);
}
Collection<User> users = userDao.getAllUsers();
req.setAttribute("model",users);
req.getRequestDispatcher("users.jsp").forward(req,resp);
/*********End*********/
}
}