昨天晚上出現的500錯誤緣由在於驗證碼沒有獲取到,獲取驗證碼是應該獲取的是共享域中的驗證碼,而我把獲取值得鍵給寫成了jsp中的鍵,而不是內存生成圖片中,而後把圖片上傳到共享域中的鍵。這兩個鍵搞混了,因此獲取不到驗證碼。html
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>login</title> <script> window.onload = function(){ document.getElementById("img").onclick = function(){ this.src="/day14/checkCodeDemo4?time="+new Date().getTime(); } } </script> <style> div{ color: red; } </style> </head> <body> <form action="/day14/loginServlet" method="post"> <table> <tr> <td>用戶名</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密碼</td> <td><input type="password" name="password"></td> </tr> <tr> <td>驗證碼</td> <td><input type="text" name="checkCode"></td> </tr> <tr> <td colspan="2"><img id="img" src="/day14/checkCodeDemo4"></td> </tr> <tr> <td colspan="2"><input type="submit" value="登陸"></td> </tr> </table> </form> <div><%=request.getAttribute("cc_error") == null ? "" : request.getAttribute("cc_error")%></div> <div><%=request.getAttribute("login_error") == null ? "" : request.getAttribute("login_error") %></div> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1><%=request.getSession().getAttribute("user")%>,歡迎您</h1> </body> </html>
package com.data; import javax.imageio.ImageIO; 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.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; @WebServlet("/checkCodeDemo4") public class checkCode extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int width = 100; int height = 50; //建立對象,該對象能夠在內存中生成圖片 //BufferedImage是Image的一個子類,Image和BufferedImage的主要做用就是將一副圖片加載到內存中。 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//這個類第一次接觸,好陌生,好抽象 //圖片加載進內存了,就盡情的虐待他吧 //美化圖片 //填充背景色 Graphics g = image.getGraphics(); g.setColor(Color.PINK); g.fillRect(0,0,width,height); //畫邊框 g.setColor(Color.BLUE); g.drawRect(0,0,width-1,height-1); String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; // 寫驗證碼 Random ran = new Random(); StringBuilder sb = new StringBuilder();//初始容量爲16字節 //隨機生成4個字符 for(int i = 1;i<=4;i++){ int index = ran.nextInt(str.length()); char ch = str.charAt(index); sb.append(ch);//這個方法能夠接收任意類型的數據 g.drawString(ch+"",width/5*i,height/2);//ch表明要繪製的字符,x,y表明繪製的座標 } String value_checkCode_session = sb.toString();//把字符變成字符串 System.out.println(value_checkCode_session); //建立對象,使用方法將驗證碼存入session req.getSession().setAttribute("key_checkCode_session",value_checkCode_session);//把數據放入共享域 g.setColor(Color.GREEN);//線條的顏色 //畫10條幹擾線 for (int i = 0 ; i< 10 ;i++){ int x1 = ran.nextInt(width); int x2 = ran.nextInt(width); int y1 = ran.nextInt(height); int y2 = ran.nextInt(height); g.drawLine(x1,y1,x2,y2);//參數是座標 } //將圖片輸出到頁面上 ImageIO.write(image,"jpg",resp.getOutputStream()); } }
package com.data; 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 javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //根據鍵獲取值 String username = request.getParameter("username"); String password = request.getParameter("password"); String checkCode = request.getParameter("checkCode");//這裏的checkCode是用戶輸入的驗證碼 //要先看看獲取的驗證碼是否正確 //先得取得內存中生成的驗證碼, HttpSession session = request.getSession();//建立HttpSession對象 String checkCode1 = (String)session.getAttribute("key_checkCode_session");//這裏的checkCode是內存中得到的驗證碼 //把得到的驗證碼刪除,確保只使用一次 session.removeAttribute("checkCode_session"); //判斷驗證碼是否正確,忽略大小寫 if(checkCode1!=null&&checkCode1.equalsIgnoreCase(checkCode)){ //驗證碼正確 //判讀用戶名和密碼是否一致 if("zhangsan".equals(username)&&"123".equals(password)){ //存儲信息, session.setAttribute("user",username); //重定向到success.jsp頁面 response.sendRedirect(request.getContextPath()+"/success.jsp"); }else{ //儲存信息到request request.setAttribute("login_error","用戶名或密碼錯誤"); //轉發都登陸頁面 request.getRequestDispatcher("/login.jsp").forward(request,response); } } else{ //驗證碼不一致 //存儲提示信息到request request.setAttribute("cc_error","驗證碼錯誤,請重試"); //轉發到登陸頁面 request.getRequestDispatcher("/login.jsp").forward(request,response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
1.指令java
做用:用於配置JSP頁面,導入資源文件數據庫
格式:<%@ 指令名稱 屬性名1=屬性值1 屬性名2=屬性值2 ...%>數組
指令分類:瀏覽器
1.page :配置JSP頁面的session
- contentType:等同於response.setContentType(),設置響應體的mime類型
- import:導包
- errorPage:當前頁面發生異常後,會自動跳轉到指定的錯誤頁面
- isErrorPage:標識當前是不是錯誤頁面,true,是,可使用內置對象exception,false:否,默認值,不能夠設置內置對象exception
2.include:頁面包含的,導入頁面的資源文件mvc
- <% @include file="要導入的資源文件名稱"%>
3.taglib :導入資源app
- <%@ taglib prefix="c" uri="文件的地址" %>
- prefix :前綴,自定義的
在jsp頁面中不須要建立,直接使用的對象,一共有9個dom
變量名 | 真實類型 | 做用 |
---|---|---|
pageContext | PageContext | 當前頁面共享數據,還能夠獲取其餘八個內置對象 |
request | HttpServletRequest | 一次請求訪問的多個資源 |
session | HttpSession | 一次會話的多個請求間 |
application | ServletContext | 全部用戶間共享數據 |
response | HttpServletResponse | 響應對象 |
page | Object | 當前頁面的對象 |
out | jspWriter | 輸出對象,數據輸出到頁面上 |
config | ServletConfig | Servlet的配置對象 |
exception | Throwable | 異常對象 |
M:Model,模型。JavaBean,完成具體的業務操做,如,查詢數據庫,封裝對象jsp
V:view,視圖,JSP,展現數據
C:Controller,控制器。Servlet,獲取用戶輸入,調用模型,將數據交給視圖進行展現
1.概念:Expression Language 表達式語言
2.做用:替換和簡化jsp頁面中java代碼的編寫
3.語法:${表達式}
4.注意:jsp默認支持el表達式,瀏覽器會解析el表達式,若是讓瀏覽器忽略jsp頁面中全部的el表達式能夠設置jsp中page指令中:isELIgnored="true"會 忽略當前jsp頁面中全部的el表達式,或者使用${表達式}:會忽略當前這個el表達式。
1.el表達式只能從域對象中獲取值
2.語法:
對象格式:${域名稱.鍵名.屬性名}
List集合格式:${域名稱.鍵名[索引]}
Map集合格式:${域名稱.鍵名.key名稱}或者${域名稱.鍵名["key名稱"]}
package com.data.jsp; import java.text.SimpleDateFormat; import java.util.Date; public class User { private String name; private int age; private Date birthday; //邏輯視圖 //須要顯示年月日的日期格式須要建立一個方法 public String getBirStr(){ //格式化日期 if(birthday !=null){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); //返回字符串 return sdf.format(birthday); }else{ return ""; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
<%@ page import="com.data.jsp.User" %> <%@ page import="java.util.*" %><%-- Created by IntelliJ IDEA. User: Yuan Date: 2019/6/11 Time: 18:20 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--給User設置值--%> <% User user = new User(); user.setName("張三"); user.setAge(23); user.setBirthday(new Date()); // 共享域 request.setAttribute("u",user); //建立List集合 List list = new ArrayList(); list.add("雲想衣裳花想容"); list.add("春風扶檻露華濃"); // 添加對象進去看看 list.add(user); request.setAttribute("list",list); //建立Map集合 Map map = new HashMap(); map.put("sname","李四"); map.put("gender","男"); //添加用戶進去看看 map.put("user",user); request.setAttribute("map",map); %> <h3>el獲取對象中的值</h3> <%--獲取對象的值須要經過對象的方法來獲取--%> <%--setter或getter方法,去掉set或get,在將剩餘部分,首字母變小寫--%> ${requestScope.u.name}<br> ${requestScope.u.age}<br> ${requestScope.u.birthday}<br> ${requestScope.u.birStr}<br> <h3>el獲取List集合中的值</h3> ${requestScope.list}<br><%--獲取的是整個集合--%> ${requestScope.list[0]}<br> ${requestScope.list[1]}<br> ${requestScope.list[2].name}<br> ${requestScope.list[2].age}<br> <h3>el獲取Map集合中的值</h3> ${requestScope.map}<br> ${requestScope.map.sname}<br> ${requestScope.map.gender}<br> ${requestScope.map.user.name}<br> </body> </html>
功能:用於判斷字符串,集合,數組對象是否爲null而且長度是否爲0
格式:{empty list}
el表達式中有11個隱式對象
pageContext:獲取jsp其餘八個內置對象
${pageContext.request.contextPath}:動態獲取虛擬目錄