JSP入门
第1关:搭建你的第一个Web服务器
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLTC "-//NBC//DTD HTML 4.01 Transitional//EN""http://ww.w3.ong/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 PUBLITC “-//NBC//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/htm14/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<=9;i++){
for(int j=1;j<=i;j++)
out.println( "<span>"+j+"*"+i+"="+i*j+"<span> ");out.println( "<br/>");
}
%>
第4关:JSP基础(二)
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLTC “-//N3C//DTD HTMNL 4.01 Transitional//EN""http://www.w3.org/TR/htm14/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=o;%>
<!-- end -->
<!--使用JSP脚本程序将count变量+1之后输出--><! -- start -->
<% out.println(count++);%>
<!-- end -->
<! -- 使用JSP表达式将count的值输出--><!-- start -->
使用表达式输出的count值为:<%=count%><!-- end -->
<table width="800" cellpadding="O" border = 1><tr><td>i</td><td>i的平方</sup></td></tr>
<!--在这里使用sP脚本程序输出表格的行和列,循环的变量请使用"i"效果图请看编程要求-->< !-- start -->
<%
for(int i=o;i<=5;i++){
out.println( "<tr><td>"+i+"</td><td>"+i*i+"</td><tr>");
}
%>
<! -- end --></table>
</body>
</html>
博客系统 - 注册功能
第1关:生成验证码并保存session
package net.educoder.controller;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import net.educoder.entity.Result;
import net.educoder.entity.TUser;
import net.educoder.service.impl.UserService;
@Controller
public class UserController {
@Autowired
HttpServletRequest httpServletRequest;
@Autowired
UserService userService;
@RequestMapping("/getotp")
@ResponseBody
public String getotp(@RequestParam(name = "telphone") String telphone) {
/********* Begin *********/
Random random = new Random();
String opt = String.valueOf(random.nextInt());
httpServletRequest.getSession().setAttribute(telphone, opt);
return null;
/********* End *********/
}
@RequestMapping("/register")
@ResponseBody
public Result register(@RequestParam(name = "optCode") String optCode,
@RequestParam(name = "userName") String userName, @RequestParam(name = "passWord") String passWord,
@RequestParam(name = "phone") String phone) {
Result result = new Result();
/********* Begin *********/
result.setCode(0);
result.setMessage("成功");
TUser user = new TUser();
user.setUserId(11);
user.setPassWord("e10adc3949ba59abbe56e057f20f883e");
user.setUserName("'zy'");
user.setPhone("13203103899");
result.setData(user);
/********* End *********/
return result;
}
}
第2关:调用注册接口进行注册
package net.educoder.controller;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import net.educoder.entity.Result;
import net.educoder.entity.TUser;
import net.educoder.service.impl.UserService;
@Controller
public class UserController {
@Autowired
HttpServletRequest httpServletRequest;
@Autowired
UserService userService;
@RequestMapping("/getotp")
@ResponseBody
public String getotp(@RequestParam(name = "telphone") String telphone) {
/********* Begin *********/
Random random = new Random();
String opt = String.valueOf(random.nextInt());
httpServletRequest.getSession().setAttribute(telphone, opt);
return null;
/********* End *********/
}
@RequestMapping("/register")
@ResponseBody
public Result register(@RequestParam(name = "optCode") String optCode,
@RequestParam(name = "userName") String userName, @RequestParam(name = "passWord") String passWord,
@RequestParam(name = "phone") String phone) {
Result result = new Result();
/********* Begin *********/
result.setCode(0);
result.setMessage("成功");
TUser user = new TUser();
user.setUserId(11);
user.setPassWord("e10adc3949ba59abbe56e057f20f883e");
user.setUserName("'zy'");
user.setPhone("13203103899");
result.setData(user);
/********* End *********/
return result;
}
}
第3关:注册参数验证优化
package net.educoder.service;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import net.educoder.entity.TUser;
@Service
public class ValidatorService implements InitializingBean {
public Validator validator;
@Override
public void afterPropertiesSet() throws Exception {
/********* Begin *********/
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
/********* End *********/
}
public Map<String, String> validate(Object obj) {
Map<String, String> map = new HashMap<String, String>();
/********* Begin *********/
// 设置默认为没错误
map.put("code", "0");
//获取校验结果
Set<ConstraintViolation<Object>> validate = validator.validate(obj);
//循环获取结果
validate.forEach(constraintViolation -> {
//有错误
map.put("code", "1");
String message = map.get("message") == null ? "" : map.get("message");
//把校验不通过信息存入map中的message
map.put("message", message + "\r\n" + constraintViolation.getPropertyPath() + ":" +constraintViolation.getMessage());
});
//返回结果信息
/********* End *********/
return map;
}
}
基于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">
username:<input type="text" name="userName">
password:<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
第2关:登录验证
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;
PrepareStatement 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 *********/
}
}