JSP全称是Java Server Pages(Java做事器页),是一种基于Java措辞的网络做事器页面技能。在传统的HTML页面文件中加入Java程序片段和JSP标记就构成了JSP网页。JSP是目前动态网站开拓领域的一种主流技能。 JSP软件开拓的入门条件是要搭建好编程环境。这个中步骤多问题也多,诸多困难成为初学者的拦路虎。本文目的便是帮助初学者战胜困难。详细记录编程环境搭建步骤、碰着的各种问题及办理方法,末了给出示例程序源代码。读者按照本文一步一步操作,就能快速的入门JSP软件开拓。
目录
JSP编程环境搭建安装和运行软件时的问题及办理方法示例程序的源代码附注正文
JSP编程环境搭建
依次安装JDK,Tomcat,MyEclipse,MySQL,jdbc,要特殊把稳软件版本适配。我安装的是JDK1.6 , Tomcat 6.0.32, MyEclipse 10, MySQL 6.0, MySQL Connector/J 5.17.
安装JDK及设置首先下载并安装JDK。 其次还要设置3个环境变量:JAVA_HOME , CLASS_PATH , Path 。
STEP 1:右键选择 此电脑→属性→高等系统设置→高等→环境变量→系统变量
STEP 2:系统变量→新建
变量名:JAVA_HOME
变量值:D:\Program Files\Java\jdk1.6.0_39(此处改为自己的jdk安装目录)
STEP 3:系统变量→新建
变量名:CLASS_PATH
变量值:.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar(把稳最前面有一点)
STEP 4:在系统变量中找到 Path 变量,选择编辑
在变量值末了输入 %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; (把稳如果原来Path的变量值末端没有;号,就先输入;号再输入上面的代码)
末了验证。 点击“开始”输入cmd,在打开的命令行窗口输入 java -version , 如果显示java 版本号则表示jdk安装与设置全部成功。如下图所示。
安装Tomcat
安装结束在D:\Program Files\apache-tomcat-6.0.32
\bin 目录找到并运行 startup.bat, 运行结果如下图。
然后打开浏览器,输入 http://localhost:8080 如果显示Tomcat主页则表示安装成功。
安装MyEclipse
安装成功之后运行MyEclipse。如图所示。
设置MyEclipse的Tomcat选项
点击Window->Preferences->MyEclipse->Servers->Tomcat
->Tomcat6.x 选中Enable, 设置Tomcat home directory等为D:\Program Files\apache-tomcat-6.0.32(此处改为自己的Tomcat安装目录)
安装MySQL
安装成功之后MySQL就作为WINDOWS的一项做事自动运行不须要手动启动。点击“开始”输入“做事”,在打开的做事窗口可以看到MySQL正在运行。
在命令行窗口输入 mysql -u root -p
在涌现Enter password:后输入密码,例如123456(这个密码是安装时设置的)。涌现提示符mysql>就表示数据库登录成功。可以用SQL语句在此窗口对MySQL进行各种数据库操作。例如新建/删除/查询数据库,新建/删除/查询表,新增删除修正查询数据。
数据库创建脚本:
CREATE DATABASE bookstore;
use bookstore;
CREATE TABLE book (
id CHAR(20) PRIMARY KEY ,
name VARCHAR(50) NULL ,
writer VARCHAR(30) NULL ,
press VARCHAR(30) NULL ,
price FLOAT NULL
);
insert into book (id,name,writer,press,price) values('10001','Python Programming Course','Lu Xuesong','Southeast Univ Press', 58);
insert into book values('10002','C Language Programming','Zhou Caiying','Qinghua Univ Press', 30);
insert into book values('10003','JSP Application Technology','Ma Jianhong','Qinghua Univ Press',99);
下载 mySQL jdbc
下载mysql-connector-java-5.1.7-bin(此处改为和自己mysql版本适配的版本)。一样平常下载文件是一个压缩包,解压缩为一个文件夹。找到个中名为mysql-connector-java-5.1.7-bin.jar的文件复制一份保存在自己项目的\WEB-INF\lib目录下。
2 安装和运行软件时的问题及办理方法
1)版本不适配
办理方法:改成适配版本JDK1.6 , Tomcat 6.0.32, MyEclipse 10, MySQL 6.0, MySQL Connector/J 5.17.
2)报错找不到jdbc driver
问题缘故原由:1.jdbc版本不对 2.项目目录\WEB-INF\lib之中短缺jdbc包。
把稳:和mySQL 6.0 版本适配的是 MySQL Connector/J 5.17它们都是适配JDK 1.6的。而不是 MySQL Connector/J 6.0。
办理方法:(1)下载MySQL Connector/J 5.17 (2) 复制一份mysql-connector-java-5.1.7-bin.jar文件到项目目录\WEB-INF\lib下。
3)tomcat不能启动,端口被占用,
Step 1, 在命令行窗口输入 netstat -ano| findstr 8080 找出占用8080端口的进程。
Step 2, 输入 taskkill /pid 8848 /f 逼迫结束该进程.
如下图所示。
然后可以运行 startup.bat启动tomcat.
4)安装mysql失落败,要彻底打消以前安装mysql的残留痕迹,
如果是重装MySQL,安装之前先要卸载原来的MySQL,还要彻底打消原来安装的残留痕迹。否则就会安装失落败,提示你输入current root password.
办理方法:
Step 1 卸载原来的MySQL
Step 2 关闭已经启用的MySQL做事
Step 3 删除安装文件夹(C盘与D盘都有文件)
删除 C:\programData\MySQL文件夹。
Step 4 修正注册表:打开注册表(win+r 键入regedit)
删除HKEY_LOCAL_MACHAINE\SEYSETIM\(current)controlset001(002)\service\mysql所有键值。
删除HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog\Application\MySQL 目录
删除HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Services\Eventlog\Application\MySQL 目录
删除HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application\MySQL 目录
注:注册表中的ControlSet001,ControlSet002,不一定是001和002,可能是ControlSet005、006之类,删除的时候,都删除就可以
5)mysql数据库的表查询结果显示中文乱码,未办理。非实质问题先放下.
3 示例程序的源代码
文件清单 主页 :index.jsp, 数据库查询页: booklist.jsp, 利用javabean页: usingbean.jsp, 利用javaservlet页: usingservlet.jsp, javabean文件:mybean.java,
servlet文件:Testservlet.java.
图1. MVC架构图
index.jsp
<%@ page language="java" import="java.util." contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%--
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>主页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<% out.println("<h3><font color='blue'>This is my first JSP page.</font></h3>"); %> <br>
<h1><span style='background-color:yellow'>主页</span></h1>
<hr>
<ol>
<li><a href="booklist.jsp">JSP访问数据库</a></li>
<li><a href="submit1.jsp">JavaBean利用示例</a></li>
<li><a href="usingservlet.jsp">Servlet利用示例</a></li>
</ol>
</body>
</html>
Booklist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.sql."%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JSP连接数据库</title>
</head>
<body>
<h1><font color='red'>图书列表</font></h1><hr>
<div style="background-color:yellow">数据库:bookstore , 表:book</div>
<br>
<%
final String DBDRRIVER = "com.mysql.jdbc.Driver" ;
final String DBURL = "jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=UTF-8" ;
final String DBUSERNAME = "root" ;
final String DBPASSWORD = "123456" ;
Connection conn = null ;
String sql = null ;
try{
Class.forName(DBDRRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSERNAME,DBPASSWORD) ;
}catch(Exception e){
out.println("<h3><font color='red'>"+e.toString()+"</font></h3>");
}
try{
sql = "select from book;" ;
Statement stmt=conn.createStatement();
ResultSet aa=stmt.executeQuery(sql);
out.println("<table border=2 cellpadding=5><tr bgcolor='#CCCCFF'><th>编号</th><th>书名</th><th>作者</th><th>出版社</th><th>定价</th></tr>");
while(aa.next())
{ int ID=aa.getInt(1);
String name=aa.getString(2);
String writer=aa.getString(3);
String press=aa.getString(4);
float price=aa.getFloat(5);
out.println("<tr><td>"+ID+"</td><td>"+name+"</td><td>"+writer+"</td><td>"+press+"</td><td>"+price+"</td></tr>");
}
out.println("</table>");
aa.close();
stmt.close() ;
conn.close() ;
}catch(Exception e){
out.println("<h3>访问数据库出错!</h3>");
out.println("<h3>"+ e +"</h3>");
e.printStackTrace();
}
%>
<br><br>
<a href="index.jsp">返回主页</a>
</body>
</html>
Submit1.jsp
<%@ page language="java" import="java.util." contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>输入表单</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1><font color='red'>输入用户信息</font></h1><hr>
<form action="usingbean.jsp" accept-charset="UTF-8" method="post" >
<table border=2 cellpadding=5>
<tr><th>姓名</th><td><input type="text" name="name"></td></tr>
<tr><th>电话</th><td><input type="text" name="tel"></td></tr>
<tr><th>邮箱</th><td><input type="text" name="email"></td></tr>
<tr><td colspan=2><input type="submit" value="提交">
<input type="reset" value="取消"></td></tr>
</table>
</form>
<a href="index.jsp">返回主页</a>
</body>
</html>
usingbean.jsp
<%@ page language="java" import="java.util." contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>利用JavaBean页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="user" scope="page" class="jsp.test.mybean" />
<jsp:setProperty name="user" property="" />
<h1><font color='blue'>显示用户信息</font></h1><hr>
<h3>利用javabean页面</h3>
<table border=0 cellpadding=5>
<tr><th>姓名</th><td><%=user.getName()%></td></tr>
<tr><th>电话</th><td><%=user.getTel()%></td></tr>
<tr><th>邮箱</th><td><%=user.getEmail()%></td></tr>
</table>
<br>
<a href="submit1.jsp">返回submit.jsp</a>
</body>
</html>
mybean.java
package jsp.test;
/ @author Administrator /
public class mybean {
public mybean() {
// TODO Auto-generated constructor stub
}
private String name;
private String tel;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
usingservlet.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>调用Servlet页</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
<!--
.STYLE2 {
font-family: "Courier New", Courier, monospace;
font-size: 16px;
font-weight: bold;
color: red;
}
-->
</style>
</head>
<body>
<h1><font color='red'> 调用Servlet的页面</font></h1><hr>
<ul>
<li><a href="./servlet/TestServlet?number=1">功能页面1</a></li>
<li><a href="./servlet/TestServlet?number=2">功能页面2</a></li>
</ul>
<a href=index.jsp>返回主页</a>
</body>
</html>
TestServlet.java
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
/
Constructor of the object.
/
public TestServlet() {
super();
}
/
Destruction of the servlet. <br>
/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/
The doGet method of the servlet. <br>
This method is called when a form has its tag value method equals to get.
@param request the request send by the client to the server
@param response the response send by the server to the client
@throws ServletException if an error occurred
@throws IOException if an error occurred
/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String number=request.getParameter("number");
String pageInfo="This is function page "+number;
request.setAttribute("pageInfo", pageInfo);
if(number.equals("1"))
this.getServletConfig().getServletContext().getRequestDispatcher("/page1.jsp").forward(request, response);
else
this.getServletConfig().getServletContext().getRequestDispatcher("/page2.jsp").forward(request, response);
}
/
The doPost method of the servlet. <br>
This method is called when a form has its tag value method equals to post.
@param request the request send by the client to the server
@param response the response send by the server to the client
@throws ServletException if an error occurred
@throws IOException if an error occurred
/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
/
Initialization of the servlet. <br>
@throws ServletException if an error occurs
/
public void init() throws ServletException {
// Put your code here
}
}
page1.jsp
<%@ page language="java" import="java.util." contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>功能页1</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1><font color='green'>功能页面<%=request.getParameter("number")%></font></h1><hr>
<h2><%=request.getAttribute("pageInfo")%></h2>
<table border=2 cellpadding=5 bgcolor='yellow'>
<tr><th> </th><td> </td><td> </td></tr>
<tr><th> </th><td> </td><td> </td></tr>
</table>
<br>
<a href="usingservlet.jsp">返回调用servlet页面</a>
</body>
</html>
Page2.jsp
<%@ page language="java" import="java.util." contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>功能页2</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1><font color='blue'>功能页面<%=request.getParameter("number")%></font></h1><hr>
<h2><%=request.getAttribute("pageInfo")%></h2>
<table border=1 cellpadding=5 bgcolor='#cccccc'> <tr><th> </th><td> </td></tr> <tr><th> </th><td> </td></tr> <tr><th> </th><td> </td></tr>
</table>
<br>
<a href="usingservlet.jsp">返回调用servlet页面</a>
</body>
</html>
4 附注
1)EJB 与JavaBean 差异
这二者是不同的但名称相似随意马虎稠浊。EJB全称是Enterprise JavaBean。EJB是SUN公司的做事器端组件模型,最大的用途是支配分布式运用程序,是JavaEE(Java Platform, Enterprise Edition)规范的一部分,用于开拓企业级运用程序。EJB须要运行在J2EE做事器如Websphere,weblogic,Jboss之上。 而JavaBean便是普通的Java类。 Tomcat支持JavaBean和Servlet 但不支持EJB。
2)jdk中的示例程序
JDK自带一些很好的示例程序。初学者可以从中学习或直接拿来修正之后为我所用。示例程序目录在D:\Program Files
\Java\jdk1.6.0_39\demo之中。(此处改本钱身的JDK安装路径+\demo)如图所示。把稳运行Applet之前浏览器要改设置。
打开包含Java Applet的html页面显示效果不正常。如图所示。
我们须要把浏览器的模式从极速模式改为兼容模式。
兼容模式涌现黄色警告信息。单击它在弹出菜单选许可阻挡内容。在弹出的安全警告框中点击“是”按钮。末了我们看到Applet的运行结果。(一个3D分子模型,移动鼠标可以掌握其迁徙改变)demo文件夹有很多有趣的程序等待我们探索。例如,冒泡排序与快速排序的动画演示,很多平面和3D的图形和动画程序。
声明:本文出于传授教化的公益目的,部分图片来自网络。如有图片侵权望奉告,必删图片。
参考文献
[1]马建红. JSP运用与开拓技能(第3版). 清华大学出版社. 2024
[2]滴水梵音. JDK环境变量配置方法.https://blog.csdn.net/binbingg/article/
details/38260167
[3]学了Java不会用. 办理:Tomcat 8080 端口被占用的问题的方案. https://blog.csdn.net/2302_81961635/article/details/135847844
[4]伊成. MYSQL安装时办理要输入current root password的办理方案.
https://blog.csdn.net/lvoyee/article/details/104030272
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
本账号“星辰博士讲数学”专注大学理工科教诲科研和数学思维演习。
如果本文对你有用,请关注作者。欢迎点赞评论转发收藏。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~