首先,在创建之前必须要做的事,更改编码。
在jsp页面中输出数组中的元素案例,使用for循环和Arrays.toString()方法
介绍两种导包方式:
1.<%@ page language="java" import = "java.util.Arrays" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
2.<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "java.util.Arrays" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "java.util.Arrays" %>
第一个jsp页面
<%
int[] arr = new int[]{1,2,3,4};
%>
Java和jsp混合使用
<%
for(int i=0;i
<%=Arrays.toString(arr) %>
java和html混合使用的小案例
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="java.util.Map.*" %>
jsp和java的混合使用
<%
ArrayList list = new ArrayList();
list.add("玩具");
list.add("文具");
list.add("书籍");
HashMap map = new HashMap<>();
map.put(1, "玩具");
map.put(2, "文具");
map.put(3, "书籍");
%>
编号
类型
<%
for(int i=0;i
<%=i+1 %>
<%=list.get(i) %>
<%} %>
<%
Set> e = map.entrySet();
Iterator> it = e.iterator();
%>
<%
Set key = map.keySet();
for(Integer k:key){
%>
<%=map.get(k) %>
<%
}
%>
注意:每次在jsp文件中写代码前更改编码
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
登录:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登录
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登录验证
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<%
//从index.jps页面传递来的数据, 使用request来获取
String name = request.getParameter("name");
String password = request.getParameter("password");
//设置登录名是张三,密码是9999,如果输入正确跳转到welcom.jsp页面,如果输入不正确,跳转到原来的页面
if("张三".equals(name) && "9999".equals(password)){
/*
response是客户端行为,是客户端(浏览器行为)来发起跳转请求
request是服务端行为,是由服务器内部进行跳转,如果服务器跳转了的地址 浏览器不知道,所以浏览器就停留在上一个地址
*/
//response.sendRedirect("welcom.jsp?name="+name);
request.getRequestDispatcher("welcom.jsp").forward(request, response);
//注意:服务器跳转的是相对路径
}else{
response.sendRedirect("index.jsp");
}
%>
注意:用//response.sendRedirect(“welcom.jsp?name=”+name);
request.getRequestDispatcher(“welcom.jsp”).forward(request, response);跳转页面的区别;
在地址栏中:
response跳转
http://localhost:8080/web02/welcome.jsp?cu_name=???
request跳转
http://localhost:8080/web02/login1.jsp?cu_name=张三&cu_phone=9999
登陆成功后跳转的welcome.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
欢迎界面
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<%
String name = request.getParameter("name");
%>
你好!<%=name %>
登录页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登录页面
用户名或密码错误
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String err = request.getParameter("err");
//如果err传回来的值是1,则输出错误提示
if("1".equals(err)){
out.println("");
}
%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String cu_name = request.getParameter("cu_name");
String cu_phone = request.getParameter("cu_phone");
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8";
String user="root";
String password="123456";
conn = DriverManager.getConnection(url, user, password);
//sql的查询语句
String sql = "select count(*) from customer where cu_name=? and cu_phone=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,cu_name);
ps.setString(2,cu_phone);
ResultSet rs = ps.executeQuery();
rs.next();
int c = rs.getInt(1);
if(c>0){
//如果查到的值大于0则,登录成功,跳转到welcome.jsp页面
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}else{
//如果失败就跳转回登录页面输出提示
response.sendRedirect("/web03_0904/index.jsp?err=1");
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
欢迎界面
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<%
String cu_name = request.getParameter("cu_name");
%>
你好!<%=cu_name %>
注册页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
注册页面
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
表单验证 所有的不能为空 用户名不能少于3个字符 密码不能少于4个字符
js代码:
// 1 获取元素
var inputs=document.getElementsByTagName("input");
var cu_name=document.getElementsByName("cu_name")[0];
var cu_phone=document.getElementsByName("cu_phone")[0];
// 2 操作元素
function check(){
//1为空判断
for(var i=0;i
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.UUID"%>
<%
//修改字符集
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获取表单中的五个属性
String cu_id=UUID.randomUUID().toString();
String cu_name=request.getParameter("cu_name");
String cu_phone=request.getParameter("cu_phone");
String cu_gender=request.getParameter("cu_gender");
String cu_address=request.getParameter("cu_address");
//java连接数据库操作
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8";
String user="root";
String password="123456";
conn = DriverManager.getConnection(url, user, password);
String sql = "insert into customer (cu_id,cu_name,cu_phone,cu_gender,cu_address) values (?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, cu_id);
ps.setString(2, cu_name);
ps.setString(3, cu_phone);
ps.setInt(4, new Integer(cu_gender));
ps.setString(5, cu_address);
int line = ps.executeUpdate();
//写入成功就跳转到welcome.jsp页面,失败就回到登录页面
if(line>0){
response.sendRedirect("/web03_0904/welcome.jsp?cu_name="+cu_name);
}else{
response.sendRedirect("/web03_0904/index.jsp?err=2");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
%>