
jsp页面获取select的值:
测试页面代码如下:
<%@ page language="java" import="javautil" pageEncoding="GB18030"%>
<%
requestsetCharacterEncoding("GB18030");//加上这一句解决的
String path = requestgetContextPath();
String basePath = requestgetScheme() + "://"
+ requestgetServerName() + ":" + requestgetServerPort()
+ path + "/";
//存放下来菜单对应值的数组
ArrayList nu = new ArrayList();
nuadd("一");
nuadd("二");
nuadd("三");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 401 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'testselectjsp' starting page</title>
</head>
<body>
通过requestgetParameter("number")方法取得下拉框选取的值
<form method=post action="testselectjsp"> <!-- 提交给自身 -->
<select name=number>
<%
for (int i = 0; i < nusize(); i++) {
outprint("<option>" + nuget(i) + "</option>");
}
%>
</select>
<input type="submit" value="提交" name="submit">
</form>
</body>
<%
//取得提交的数字,并显示
String n = (String) requestgetParameter("number");
outprint("选的值是:" + n);
%>
</html>
运行界面:
最简单的JSP页面中的数据库 *** 作方法:
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>
<%@page import="javasql"%>
<center>
<H1> <font color="blue" size="12">管理中心</font></H1>
<HR />
<table width="80%" border="1">
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>价格</th>
<th>删除</th>
</tr>
<%
// 数据库的名字
String dbName = "zap";
// 登录数据库的用户名
String username = "sa";
// 登录数据库的密码
String password = "123";
// 数据库的IP地址,本机可以用 localhost 或者 127001
String host = "127001";
// 数据库的端口,一般不会修改,默认为1433
int port = 1433;
String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";user=" + username
+ ";password=" + password;
//
//声明需要使用的资源
// 数据库连接,记得用完了一定要关闭
Connection con = null;
// Statement 记得用完了一定要关闭
Statement stmt = null;
// 结果集,记得用完了一定要关闭
ResultSet rs = null;
try {
// 注册驱动
ClassforName("commicrosoftsqlserverjdbcSQLServerDriver");
// 获得一个数据库连接
con = DriverManagergetConnection(connectionUrl);
String SQL = "SELECT from note";
// 创建查询
stmt = concreateStatement();
// 执行查询,拿到结果集
rs = stmtexecuteQuery(SQL);
while (rsnext()) {
%>
<tr>
<td>
<%=rsgetInt(1)%>
</td>
<td>
<a href="prepareupdateID=<%=rsgetInt("ID")%>" target="_blank"><%=rsgetString(2)%></a>
</td>
<td>
<%=rsgetString(3)%>
</td>
<td>
<%=rsgetString(4)%>
</td>
<td>
<a href="deleteID=<%=rsgetInt("ID")%>" target="_blank">删除</a>
</td>
</tr>
<%
}
} catch (Exception e) {
// 捕获并显示异常
eprintStackTrace();
} finally {
// 关闭我们使用过的资源
if (rs != null)
try {
rsclose();
} catch (Exception e) {}
if (stmt != null)
try {
stmtclose();
} catch (Exception e) {}
if (con != null)
try {
conclose();
} catch (Exception e) {}
}
%>
</table>
<a href="insertjsp">添加新纪录</a>
</center>
使用JAVA后台代码取得WEBROOT物理路径,可以有如下两种方式:
1、使用JSP Servlet取得WEB根路径可以用requestgetContextPath(),相对路径requestgetSession()getServletContext()getRealPath("/"),它们可以使用我们很容易取得根路径。
2、如果使用了spring, 在WEB-INF/webxml中,创建一个webAppRootKey的param,指定一个值(默认为webapproot)作为键值,然后通过Listener,或者Filter,或者Servlet执行String webAppRootKey = getServletContext()getRealPath("/"); 并将webAppRootKey对应的webapproot分别作为Key,Value写到System Properties系统属性中。之后在程序中通过SystemgetProperty("webapproot")来获得WebRoot的物理路径。
具体示例代码如下:
webxml
<xml version="10" encoding="UTF-8">
<web-app version="24"
xmlns=">
你的jsp页面中有无<base href="<%=basePath%>">?
如果页面中有<base href="<%=basePath%>">,则<link href="css/indexcss" rel="stylesheet" type="text/css" />
如果页面中没有<base href="<%=basePath%>">,则<link href="/css/indexcss" rel="stylesheet" type="text/css" />
以上就是关于如何将html格式的程式码翻译成Java程式码在myeclipse环境下执行呢全部的内容,包括:如何将html格式的程式码翻译成Java程式码在myeclipse环境下执行呢、在jsp页面<base href="<%=basePath%>">根本不起作用的原因是什么、python 新浪微博爬虫,求助等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)