入门

简单的jsp文件

<%--
Created by IntelliJ IDEA.
User: e550
Date: 2017/1/9
Time: 23:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %> <html>
<head>
<title>欢迎</title>
</head>
<body>
欢迎学习Java web知识!
现在的时间是:
<%out.println(new java.util.Date());%>
</body>
</html>

声明

<%--
Created by IntelliJ IDEA.
User: e550
Date: 2017/1/9
Time: 23:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %> <html>
<head>
<title>声明示例</title>
</head>
<%!
// 声明一个整型变量
public int count;
// 声明一个方法
public String info() {
return "hello java";
}
%> <body>
<%
// 输出变量
out.println(++count); // 加1后输出1
// 输出方法的返回值
out.println(info());
%>
</body>
</html>

变量输出

<%--
Created by IntelliJ IDEA.
User: e550
Date: 2017/1/9
Time: 23:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %> <html>
<head>
<title>输出表达式</title>
</head>
<%!
// 声明一个整型变量
public int count;
// 声明一个方法
public String info() {
return "hello java";
}
%> <body>
<%=
count++
%> <%=
info()
%>
</body>
</html>

小脚本测试

<%--
Created by IntelliJ IDEA.
User: e550
Date: 2017/1/9
Time: 23:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %> <html>
<head>
<title>小脚本测试</title>
</head> <body>
<table bgcolor="#bdb76b" border="1" width="300px">
<%
for(int i=0;i<10;i++) {
%>
<tr>
<td>循环值:</td>
<td><%=i%></td>
</tr>
<%
}
%>
</table>
</body>
</html>

连接数据库

<%--
Created by IntelliJ IDEA.
User: e550
Date: 2017/1/9
Time: 23:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="" %>
<%@ page import="java.sql.*" %> <html>
<head>
<title>小脚本测试</title>
</head> <body>
<%
// 注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_mysql","root","123456");
// 创建Statement
Statement stmt = conn.createStatement();
// 执行查询
ResultSet rs = stmt.executeQuery("select * from tp_goods_type;");
%>
<table bgcolor="#bdb76b" border="1px" width="300px">
<%
// 遍历结果集
while (rs.next())
{
%>
<tr>
<td>
<%= rs.getString(1)%>
</td>
<td>
<%= rs.getString(2)%>
</td>
</tr>
<%
}
%> </table>
</body>
</html>

这里注意了,需要在编辑器中配置,

否则无法连接数据库!

最新文章

  1. IOS 封装功能和逻辑思想
  2. Web API - Video File Streaming
  3. 使用 WordPress 插件模板开发高质量插件
  4. Redis教程(十):持久化详解
  5. 百度地图API 学习网站
  6. Error -26359: Function not allowed within a concurrent group
  7. tomcat配置多个web网站的配置详解
  8. 在C#中使用WIA获取扫描仪数据
  9. 修改vim中的tab为4个空格
  10. android一分钟学会可视化操作数据库(无需ROOT)
  11. Rest之路 -- 从第二个Rest application里面分析 Rest 方法
  12. redis 主从配置实例、注意事项、及备份方式
  13. .Net代码控制PrivateBinPath和ConfigurationFile的位置
  14. cookie——创建、获取、删除
  15. P图软件瘦脸眼睛放大和手动瘦身功能
  16. Java学习——Applet菜单
  17. 基于Docker持续交付平台建设的实践
  18. Solidworks直接打开SWB文件报错怎么办
  19. sgu 129 Inheritance 凸包,线段交点,计算几何 难度:2
  20. 1.1.4 A+B for Input-Output Practice (V)

热门文章

  1. POJ 1821 Fence(单调队列优化DP)
  2. 【转载】如何学习C++
  3. Generator 简介
  4. Effective C++ 11-17
  5. iOS 9 适配,我咋还没遇到这么多坑呢呀
  6. 双系统给ubuntu增加分区
  7. 3.IntelliJ IDEA 使用详解
  8. react --- React中state和props分别是什么?
  9. Mysql优化ibdata1大小
  10. [学习笔记]node.js中的path.extname方法