http://www.runoob.com/jsp/jsp-actions.html JSP
request.getRemoteAddr() 获取本机IP
Properties properties = System.getProperties();获取系统熟悉
JSP表达式 <%= 表达式 %>
JSP注释 <%-- 这里可以填写 JSP 注释 --%>
JSP指令
<%@ page ... %> //定义网页依赖属性,比如脚本语言、error页面、缓存需求等等
<%@ include ... %> // 包含其他文件 指令 <%@ include file="date.jsp" %> <jsp:include page="date.jsp" flush="true" /> (动作)
<%@ taglib ... %> //引入标签库的定义
JSP动作:
<jsp:useBean>动作元素 <jsp:useBean id="name" class="package.class" /> //属性:class type beanName
<jsp:getProperty>动作元素 //name 要检索的Bean属性名称。Bean必须已定义。property表示要提取Bean属性的值
<jsp:setProperty>动作元素 name property value param
<jsp:forward> 动作元素 <jsp:forward page="Relative URL" />
<jsp:element> 、 <jsp:attribute>、 <jsp:body>动作元素
<jsp:text>动作元素
JSP 过滤器:1,/opt/tomcat6_bill/webapps/zou/WEB-INF/classes/LogFilter.class
2. LogFilter.class中引入了Java类,手动加入路径touch a.sh
tomcat下运行Java代码
#!/bin/sh
javac -cp .:/opt/apache-tomcat-7.0.62/lib/annotations-api.jar:/opt/apache-tomcat-7.0.62/lib/catalina-ant.jar:/opt/apache-tomcat-7.0.62/lib/catalina-ha.jar:/opt/apache-tomcat-7.0.62/lib/catalina.jar:/opt/apache-tomcat-7.0.62/lib/catalina-tribes.jar:/opt/apache-tomcat-7.0.62/lib/ecj-4.4.2.jar:/opt/apache-tomcat-7.0.62/lib/el-api.jar:/opt/apache-tomcat-7.0.62/lib/jasper-el.jar:/opt/apache-tomcat-7.0.62/lib/jasper.jar:/opt/apache-tomcat-7.0.62/lib/jsp-api.jar:/opt/apache-tomcat-7.0.62/lib/servlet-api.jar:/opt/apache-tomcat-7.0.62/lib/tomcat7-websocket.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-api.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-coyote.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-dbcp.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-i18n-es.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-i18n-fr.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-i18n-ja.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-jdbc.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-util.jar:/opt/apache-tomcat-7.0.62/lib/websocket-api.jar *.java
3./opt/tomcat6_bill/webapps/zou/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.HelloWorldWebSocketServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/websocket/test</url-pattern>
</servlet-mapping>
</web-app>
4.查看log /opt/tomcat6_bill/logs/catalina.out
JSP 与数据库
centos7安装mysql
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
service mysqld restart
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/test"
user="root" password="zouhuiying"/>
<sql:update dataSource="${snapshot}" var="result">
INSERT INTO Employees VALUES (104, 2, 'Nuha', 'Ali');
</sql:update>
<sql:update dataSource="${snapshot}" var="count">
DELETE FROM Employees WHERE Id = ?
<sql:param value="${empId}" />
</sql:update>
<sql:query dataSource="${snapshot}" var="result">
SELECT * from Employees;
</sql:query>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.id}"/></td>
<td><c:out value="${row.first}"/></td>
<td><c:out value="${row.last}"/></td>
<td><c:out value="${row.age}"/></td>
</tr>
</c:forEach>
javabean public void setAge(int age){
this.age = age;
自定义标签
@Override
22 public void doTag() throws JspException, IOException {
23 //得到代表jsp标签体的JspFragment
24 JspFragment jspFragment = this.getJspBody();
25
26 //得到jsp页面的的PageContext对象
27 //PageContext pageContext = (PageContext) jspFragment.getJspContext();
28 //调用JspWriter将标签体的内容输出到浏览器
29 //jspFragment.invoke(pageContext.getOut());
30
31 //将标签体的内容输出到浏览器
32 jspFragment.invoke(null);
JSP的session应用与JSP操作数据库,Java重定向
<%=request.getSession().getAttribute("email")%>
<%=request.getSession().getAttribute("password")%>
<%@page import="java.sql.*"%>
<%
String sqlpassword="";
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "websocket1";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "zouhuiying";
try {
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
con.setAutoCommit(false);
st = con.createStatement();
String sql = "select password from myapp_chatusers where email ='"+emaill+"'";
rs = st.executeQuery(sql);
// rs = st.executeUpdate(sql);
// PreparedStatement pre = con.prepareStatement(sql);
// pre.executeUpdate();安全性高
out.println("No \tName");
out.print("</br>");
while (rs.next()) {
sqlpassword=rs.getString(1);
out.print(sqlpassword + " \t");
out.print("</br>");
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
<%
if (sqlpassword.equals(passwordd)){
out.print("ok");
request.getSession().setAttribute("email",emaill);
request.getSession().setAttribute("password",passwordd);
response.sendRedirect("http://192.168.139.215:8080/demo/#/sidebarRight");
}else{
out.print("wrong passwrod");
response.sendRedirect("http://192.168.139.215:8080/demo/#/forms");
}
%>
从表单和URL中获取参数值:
<%=request.getParameter("username")%>
自己设置数据:
request.setAttribute("username",username);
request.getAttribute("username");
<%String aa=(String)request.getAttribute("username");%>
<%
out.println(aa);
%>
注:从requset中取出的数据是对象要转化成字符串
session对象
<%=request.getSession().setAttribute("zou","huiying")%>
<%=request.getSession().getAttribute("zou")%>
JSP定义数组
<%
int accounts[];
accounts=new int[100];
accounts[3]=99;
out.print(accounts[3]);
%>
<%
String users[];
users=new String[100];
users[3]="zozu";
out.print(users[3]);
%>
每次刷新页面,i的值增加1
<%
if(request.getSession().getAttribute("i")==null){
request.getSession().setAttribute("i",5);
}else{
try{
Integer myi=(Integer)request.getSession().getAttribute("i");
request.getSession().setAttribute("i",(Integer)request.getSession().getAttribute("i")+1);
}catch(Exception e){
out.println(e);
}
}
out.println(request.getSession().getAttribute("i"));
%>
对象转化为字符
(String)request.getSession().getAttribute("url")