JSP Java Web's foundation

Java Web basis of Jsp

A, JSP Introduction

  • JSP stands for Java Server Pages, is a dynamic web development technology. It uses the Java code in JSP tag into the HTML page. Tags typically begin with <% in%> end.
  • JSP is a Java servlet, mainly used for the user interface portion implement Java web applications. Web developers through a combination of HTML code, XHTML code, XML elements and embedded operating JSP and commands to write JSP.
  • JSP get user input data, access databases and other data sources via a web form, and then dynamically create web pages.

Two, JSP technology

1, script
Java statements, variables, methods or expression scripts can contain any amount, as long as they are valid in the scripting language.

<% 代码片段 %>

2, Chinese coding
if we are to properly display the Chinese page, you need to add the code in JSP file header:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

3, JSP declaration
a declaration can declare one or more variables, methods, Java code, for later use.
E.g:

<%! 声明语句 %>
<%! int i=0; %>
<%! Circle a = new Circle(2.0); %>

4, JSP expression is
a scripting language expressions JSP expression contained, was first converted into a string, and then inserted into the local expression appears.
Expressions element can contain any expressions that conform to the Java language specification, but can not end with a semicolon expression

<%= 表达式%>
今天的日期是:<%= (new java.util.Date()).toLocaleString()%>

5, JSP comments

<%--注释 --%>  JSP注释,注释内容不会被发送至浏览器甚至不会被编译(常用)
<!-- 注释--!> HTML注释,通过浏览器查看网页源码时可以看见注释内容

6, JSP Action
Reference:
JSP based - action label
seven and three operation instructions JSP

7, JSP in HTML mixed

<%-- 输出三个字体大小不同的java --%>
<%
for(int i=1;i<=3;i++){%>
	<font size="<%= i %>">java</font>
<%}%>

8, JSP built-in objects
Reference:
JSP built-in objects
JSP nine built-in objects

Three, JSP form processing and redirection

1, the form of acquisition

By the JSP request gets the form of data objects
, for example:

//获取名为userName的表单数据
<%
String userName = request.getParemeter("userName");
%>

2. Redirect

By JSP response object the sendRedirect () redirection mode

<%
//跳转到login页面
response.sendRedirect("login.jsp")
%>

3, forwarding the request

Action: in the server, sends the request to other resources on the server, a request to complete a common process
to achieve forwarding: by RequestDispatcher object ForWord () method

<%
RequestDispatcher rd = request.getReuestDispatcher("welcome.jsp");
rd.forward(request,response);
%>

The difference between forward and redirect:
forward is to play the role of the server, the same request will be passed between the server resources, the client browser address bar does not show the address after the forward (a request)
to redirect the client is play a role in achieving the pages turn by sending a new request, steering address can be displayed in the address bar

Four, JSP action elements common

JSP and instruction elements is different, JSP elements play a role in the operation request processing stage. JSP action element is written using XML syntax.
Using JSP file operation can be dynamically inserted, Javabean reuse assembly, the user is redirected to another page, HTML code is generated as a Java plug.
Action element is only one syntax, which conforms to the XML standard:

<jsp:action_name atttribute="value"/>

1, page references

By reference to the contents of a page can contain another page

<!-- 在a.jsp中引用b.jsp --!>
<jsp:include page="b.jsp" flush="true"></jsp:include>

2.JSP operation JavaBean

<!-- 使用jsp:useBean 实例化对象
	id:对象名
	class:全限定类名(包名+类名)
--!>
<jsp:useBean id="student" class="cn.xxx.Student"></jsp:useBean>
<!-- 使用jsp:setProperty设置某个对象的属性 
	name:对象的标识符
	property:属性名
	value:属性值
--!>
<jsp:setProperty property="name" name="student" value="张三"/>

<jsp:getProperty property="name" name="student"/>

3. Using JSP tags to achieve forward motion

<jsp:forward page="target.jsp"></jsp:forward>
Published 33 original articles · won praise 1 · views 2276

Guess you like

Origin blog.csdn.net/qq_40805620/article/details/104223602