jsp入门

做android开发也需要一定的服务器知识,所以就尝试着了解了jsp。
jsp是通过<% %>标签插入到html中的,在里面可以使用各种的java语法知识,而且里面内置了预编译命令,比如page和include等以及许多的内置对象,比如out和request等。
jsp最终是要被编译为servlet来运行的。
1.主要学习了,jsp和servlet的跳转,jsp之间的跳转,servlet之间的跳转。
jsp之间跳转以及传递参数的代码实例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
    <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" />

</LinearLayout>

这是要跳转的jsp。

<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% String userName = request.getParameter("userName"); String passWord = request.getParameter("passWord"); out.print(userName); out.print("<br/>"); out.print(passWord); %>
</body>
</html>

这是跳转到的jsp代码。
2.下面介绍两个预编译命令:
page:

<%@ 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=UTF-8">
<title>hello world</title>
</head>
<body>
JSP表达式输出:<%="hello world" %><br/>
<%! String str = "hello world"; %>
<% //String str = "hello world"; out.println(str); %>
</body>
</html>

其中page用来定义编程语言,字符集,以及mime类型。
include:
include用来包含文件比如

<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@ include file="footer.txt" %>
<%@ include file="header.jsp" %>
</body>
</html>

主jsp
两个被包含的文件是:

<div>
this is footer.txt
</div>
<div>
this is a header.jsp.
</div>

3,。下面介绍内置的对象:
out:
输出字符串和当前缓冲区的信息:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page buffer="10kb" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    out.println("hello");
    out.print(" world");
    out.newLine();
    out.flush();
    out.clearBuffer();
    out.print("<br/>");
    out.print("获取当前缓冲区大小:"+out.getBufferSize());
    out.print("<br/>");
    out.print("获取剩余缓冲区大小:"+out.getRemaining());
%>
</body>
</html>

request:
request的主要方法是getParameter();
获得请求参数`

<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="do_register.jsp" method="post">
    用户名:<input type="text" name="userName"/>
    密码:<input type="password" name="password"/>
    <input type="submit" value="提交"/>
    </form>
</body>
</html>
<%@ 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=UTF-8">
<title>注册信息处理</title>
</head>
<body>
<jsp:useBean id="user" class="liu.peng.web.UserEntity"></jsp:useBean>
<jsp:setProperty property="userName" name="user"/>
<jsp:setProperty property="password" name="user"/>
<% String userName = request.getParameter("userName"); String password = request.getParameter("password"); out.print(userName); out.print("<br/>"); out.print(password); %>
<jsp:getProperty property="userName" name="user"/>
<jsp:getProperty property="password" name="user"/>
</body>
</html>

ok,如果有错误或者想要交流可以给我评论和留言交流。

你可能感兴趣的:(java,jsp,servlet,服务器)