html(二)登陆页面
今天开始正常上课学习HTML+CSS+JSP 嗯 前两个没讲直接上手! 老师也是很认同我们的呢~
这是第一个案例 做一个登陆页面,并利用post提交表单 传值到另一个界面接收值。
1.设置值:
<td>用户名</td><td><input type="text" name="name"></td>
2.通过“name”接收值:
String name=request.getParameter("name");
表单页面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form name="form1" method="post" action="value.jsp"> 11 <table border="0" align="center"> 12 <tr> 13 <td>用户名</td> 14 <td><input type="text" name="name"></td> 15 </tr> 16 <tr> 17 <td>密码</td> 18 <td><input type="password" name="password"></td> 19 </tr> 20 <tr> 21 <td>性别</td> 22 <td><input type="radio" name="sex" value="boy">男 <input 23 type="radio" name="sex" value="gril">女</td> 24 </tr> 25 <tr> 26 <td>家乡</td> 27 <td><select name="home"> 28 <option>福建</option> 29 <option>河南</option> 30 <option>山西</option> 31 </select></td> 32 </tr> 33 <tr> 34 <td>爱好</td> 35 <td><input type="checkbox" name="hobbies" value="sing">唱 36 <input type="checkbox" name="hobbies" value="dance">跳 37 <input type="checkbox" name="hobbies" value="rap">rap</td> 38 </tr> 39 <tr> 40 <td>自我介绍</td> 41 <td><textarea rows="" clos="" name="jieshao"></textarea></td> 42 </tr> 43 <tr> 44 <td><input type="submit"></td> 45 </tr> 46 </table> 47 </form> 48 </body> 49 </html>
接收界面:
1 <%@page import="java.util.Arrays"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 String name=request.getParameter("name"); 13 String password=request.getParameter("password"); 14 String sex=request.getParameter("sex"); 15 String home=request.getParameter("home"); 16 String[] hobbies=request.getParameterValues("hobbies"); 17 String jieshao=request.getParameter("jieshao"); 18 19 home =new String(home.getBytes("ISO-8859-1"),"UTF-8"); 20 21 %> 22 姓名:<%=name %><br> 23 password:<%=password %><br> 24 sex:<%=sex %><br> 25 home:<%=home %><br> 26 hobbies:<%=Arrays.toString(hobbies) %><br> 27 jieshao:<%=jieshao %><br> 28 29 </body> 30 </html>
其中乱码要用转码
home =new String(home.getBytes("ISO-8859-1"),"UTF-8");
继续加油,往后会能难。。
作业案例:
1.九九乘法表
<%@page import="org.apache.jasper.tagplugins.jstl.core.Out"%> <%@ 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 x = 1; x < 10; x++) { for (int y = 1; y <= x; y++) { out.print(y + "*" + x + "=" + x * y + " "); } out.print("<br/>"); } %> </body> </html>
2.求素数和:
<%@ 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> <% int sum = sum(); out.print(sum); %> <%!public int sum() { int sum = 0; int count = 0; for (int x = 1; x < 100; x++) { for (int y = 1; y < x; y++) { if (x % y == 0) { count++; } } if (count == 1) { sum = sum + x; } count=0; } return sum; }%> </body> </html>
3.验证账号密码登陆
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form name="form1" method="post"> 11 <table border="0" align="center"> 12 <tr> 13 <td>用户名</td> 14 <td><input type="text" name="name"></td> 15 </tr> 16 <tr> 17 <td>密码</td> 18 <td><input type="password" name="password"></td> 19 </tr> 20 <tr> 21 <td><input type="submit"></td> 22 </tr> 23 24 25 </table> 26 </form> 27 <% 28 String name = request.getParameter("name"); 29 String password = request.getParameter("password"); 30 31 32 int pass =0; 33 String na=null; 34 if (name!= null && !"".equals(name)&&password != null && !"".equals(password)){ 35 name = new String(name.getBytes("ISO-8859-1"), "UTF-8"); 36 pass = Integer.valueOf(password); 37 if(name.equals("Lucky")&&pass==123456){ 38 request.getRequestDispatcher("result.jsp").forward(request, response); 39 } 40 } 41 42 session.setAttribute("name",name); 43 44 %> 45 46 </body> 47 </html>
显示登陆成功
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 String name = request.getParameter("name"); 12 %> 13 姓名:<%=sname%><br> out.print("欢迎登陆"); 14 </body> 15 </html>
作者:刘认真
-------------------------------------------
新人只求记录学习生活!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!