这是最简单的一个例子,数据库要你自己建,用的是ACCESS
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JSP连接Access数据库</title>
<style type="text/css">
<!--
.style1 {
font-size: 20px;
font-weight: bold;
}
-->
</style>
</head><body>
<div align="center" class="style1">JSP连接Access数据库</div>
<br>
<hr>
<p><%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //载入驱动程序类别
Connection con = DriverManager.getConnection("jdbc:odbc:jspdata"); //建立数据库链接,jspdata为ODBC数据源名称
//建立Statement对象
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("select * from lyb"); //建立ResultSet(结果集)对象,并执行SQL语句
%>
</p>
<p align="center">NUMB1数据表中记录如下</p>
<table width="640" border="1" align="center" bordercolor="#7188e0">
<tr bgcolor="d1d1ff">
<th width="49">编号</th>
<th width="90">姓名</th>
<th width="126">E-mail</th>
<th width="221">网站</th>
<th width="80">QQ</th>
</tr>
<%
while(rs.next())
{
%>
<tr bgcolor="#f8f8f8">
<th><%= rs.getString(1) %></th>
<th><%= rs.getString(2) %></th>
<th><%= rs.getString(3) %></th>
<th bgcolor="#f6f6f8"><%= rs.getString(4) %></th>
<th><%= rs.getString(5) %></th>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
%>
</table>
<p align="center"><br>
如果您能看到表格中的数据,说明连接数据库成功!</p>
</body>
</html>
② 在JSP里怎么实现“设置首页”的功能
function setHomepage(url) { // 设置首页
if (document.all) {
document.body.style.behavior = ' url(#default#homepage) '答凳 ;
document.body.setHomePage(url);
}
else if (window.sidebar) {
if (window.netscape) {
try {
netscape.security.PrivilegeManager.enablePrivilege( " UniversalXPConnect " );
}
catch (e) {
alert( " 该操作被浏览器拒绝,如果想启用该功能,请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该清顷旅为true " );
}
}
var prefs = Components.classes[ ' @mozilla.org/preferences-service;1 ' ].getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref( '乎棚 browser.startup.homepage ' , url);
}
}
③ JSP 的首页头部 标题 关键词 网页描述应该怎么写代码啊
在head 标谨卜签里添加以下标签
1.这里写关键祥裤穗词,多个关键词可以用逗号分开
<meta http-equiv="keywords" content="纯拆keyword1,keyword2,keyword3">
2.这里写描述
<meta http-equiv="description" content="This is my page">
④ jsp怎么实现一请求首页就在首页显示从数据库中取出来的数据
既然用的是mvc模式,在jsp里直接写数据库代码就不合适。
可以把所有的java代码写伏乎返到servlet里,jsp页面只做展示和用户交互。
mvc模式的话,后台java代码也应缺饥该分层顷哗,一般的做法是分web,service,三层
⑤ jsp中网站的首页源代码
这是最简单的一个例子,数据库要你自己建,用的是ACCESS
<%@pagecontentType="text/html;charset=gb2312"language="java"import="java.sql.*"errorPage=""%>
<html>
<head>
<metancon=DriverManager.getConnection("jdbc:odbc:jspdata");//建立数据库链陆告早接,jspdata为ODBC数据源名称
//建立Statement对象
Statementstmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSetrs=stmt.executeQuery("select*fromlyb");//建立ResultSet(结果集)对象,并执行SQL语句
%>
</p>
<palign="center">NUMB1数据表中记录如友悉下</p>
<tablewidth="640"border="1"align="center"bordercolor="7188e0">
<trbgcolor="d1d1ff">
<thwidth="49">编号</th>
<thwidth="90">姓名</th>
<thwidth="126">E-mail</th>
<早雀thwidth="221">网站</th>
<thwidth="80">QQ</th>
</tr>
<%
while(rs.next())
{
%>
<trbgcolor="f8f8f8">
<th><%=rs.getString(1)%></th>
<th><%=rs.getString(2)%></th>
<th><%=rs.getString(3)%></th>
<thbgcolor="f6f6f8"><%=rs.getString(4)%></th>
<th><%=rs.getString(5)%></th>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
%>
</table>
<palign="center"><br>
如果您能看到表格中的数据,说明连接数据库成功!</p>
</body>
</html>
⑥ 如何用jsp代码把当前页设置为主页
function SetHome(obj,vrl){
try{
obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
}
catch(e){
if(window.netscape) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch (e)
{
alert("抱歉!您的浏览器不支持直接设为首页。请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]设置为“true”,点击“加入收藏”后忽略安全提示,即可设置成功。");
}
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage',vrl);
}
}
}
我的耐吵网站上就是用的这个函慎孝数
在想设置的地方进行如下方式调用:
<a onclick="宽亩稿SetHome(this,'http://www.redbar.us');return false;" href="javascript:void(0);">将红柱设为主页</a>
⑦ JSP 简单的分页 如:首页 第一页 1 2 3 4 5 6 上一页 首页 显示第7页的时候第一页隐藏这样的效果!
写一个公共的pageView.jsp,里面显示,做判断。
<!-- 分页信息 -->
<c:if test="${page.totalPage gt 1}">
<div align="right" style="margin: 0" class="pageview">
<div class="pages_numlnk" style="font-family: tahoma;">
<!-- 显示页码 -->
<c:forEach begin="${page.pageIndex.startPage }" end="${page.pageIndex.endPage}" var="i">
<c:if test="${i eq page.currentPage}">
<b>${i }</b>
</c:if>
<c:if test="${i ne page.currentPage}">
<a href="#" onclick="gotoPage0('${i }');return false;">${i }</a>
</c:if>
</c:forEach>
<!-- 总页码 -->
<span style="color: rgb(255, 69, 0);"> 共<b>${page.totalPage }</b>页 共<b>${page.totalItems }</b>条记录 </span>
<c:if test="${page.currentPage gt 1 }">
<a href="#" onclick="gotoPage0(1);return false;">[首页]</a>
<a href="#" onclick="gotoPage0(${page.currentPage - 1 });return false;">[上一页]</a>
</c:if>
<c:if test="${page.currentPage lt page.totalPage}">
<a href="#" onclick="gotoPage0(${page.currentPage + 1 });return false;">[下一页]</a>
<a href="#" onclick="gotoPage0(${page.totalPage });return false;">[尾页]</a>
</c:if>
<!-- 跳转到指定页码 -->
<input id="toPageNum" type="text" size="4" style="font: 10px; height: 15px" />
<img onclick="gotoPage0(document.getElementById('toPageNum').value)" align="absMiddle"
src="${baseUrl }/images/button_go.gif">
</div>
<script type="text/javascript">
function gotoPage0(pageNum){
if(isNaN(pageNum)){
alert("请输入正确的页码");
document.getElementById('toPageNum').focus();
return false;
}
if(pageNum < 1){
pageNum = 1;
}
if(pageNum > ${page.totalPage}){
pageNum = ${page.totalPage};
}
if(typeof(gotoPage) == "undefined"){
alert("请提供gotoPage()方法");
return false;
}
gotoPage(pageNum);
}
</script>
</div>
</c:if>
⑧ jsp页面设为首页和加入收藏怎么实现
1,加入收藏比较容易,找到那个网页顶部栏里的星星形状的那个就是把当前页面收藏了;
2,关于设置为首页,一般都是在浏览器功能菜单里设置的,
⑨ JSP域名即首页
你可以这样:做个Index首页,这个耐唤页面什么都不昌洞凯做,就颤返只用来跳转到你要的action,写个onload方法,页面一加载就执行跳转。这样看起来就是直接执行的action,我之前有个项目就这么搞的