javaweb入门

jsp3种脚本语言

<%//局部变量
    out.println("hello World"+"
"); init("chenwei"); %> <%!//全局变量,定义方法 String name ; public void init(String name){ this.name = name; } %> <%="hello"+ name %>

jsp九大内置对象

1、response:处理页面的请求 登录简单实现:
register.jsp


    
name:
password:
age:
hobbies:
篮球
足球
台球

show.jsp

注册成功,信息如下:
<%
        request.setCharacterEncoding("utf-8");
    String name = request.getParameter("uname");
    String pass = request.getParameter("upass");
    int age = Integer.parseInt(request.getParameter("uname"));
    String[] hobbies = request.getParameterValues("hobbies");
%>
name:<%=name %>
pass:<%=pass %>
age:<%=age %>
hobbies:
<% if(hobbies!=null){ for( String hobby:hobbies){ out.print(hobby+" "); } } %>

2.请求转发和重定向

request.setCharacterEncoding("utf-8");
String name = request.getParameter("uname");
String pwd = request.getParameter("upwd");
if(name.equals("cwi")&& pwd.equals("123")){
    //重定向:2次请求 不携带数据 浏览器地址栏不改变
    //response.sendRedirect("info.jsp");
    //请求转发:1次请求 携带数据 浏览器地址栏改变
    request.getRequestDispatcher("info.jsp").forward(request, response);
}else{
    out.print("login fail");
}

3.Cookie的简单使用

//服务端
<%
Cookie cookie1 = new Cookie("name","zs");
Cookie cookie2 = new Cookie("pwd","123");
response.addCookie(cookie1);
response.addCookie(cookie2);
response.sendRedirect("result.jsp");
%>
//客户端
Cookie[] cookies = request.getCookies();
for (Cookie cookie:cookies){
    out.print(cookie.getName()+"--"+cookie.getValue()+"
"); }

你可能感兴趣的:(javaweb入门)