jsp編程題
⑴ 簡單JSP編程題目怎麼做呢
index.jsp代碼<%@ page language="java" import="java.util.*" 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>My JSP 'index.jsp' starting page</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>
<form action="MyJsp.jsp" method="post" name="myForm">
<center>
<span style="font-size:12px; color:black;">請輸入內容</span>
<input type="text" name="uName" style="width: 150px; height: 20px; "/>
<input type="submit" value="確定" name="but"/>
</center>
</form>
</body>
<html>
Myjsp.jsp代碼
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String uName=request.getParameter("uName");
if(uName==""){
out.println("不能為空");
}
out.println(uName);
%>
⑵ jsp編程題
就好了,完整代碼如下,記得jar驅動放好,如果輸出的是內存地址的話就證明配置沒問題,不然就是你的操作或者配置問題了:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL="jdbc:sqlserver://localhost:1433;DATABASENAME=managerNews";
private static final String UNAME="sa";
private static final String UPASSWORD="sa";
/*
* 連接資料庫
*/
public Connection getConnection(){
Connection conn=null;
try {
Class.forName(DRIVER_CLASS);
conn=DriverManager.getConnection(URL,UNAME,UPASSWORD);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/*
* 關閉conn,ps,rs,釋放資源
*/
public void closeAll( Connection conn, PreparedStatement ps, ResultSet rs ) {
/* 如果rs不空,關閉rs */
if(rs != null){
try catch (SQLException e)
}
/* 如果pstmt不空,關閉pstmt */
if(ps!= null){
try catch (SQLException e)
}
/* 如果conn不空,關閉conn */
if(conn != null){
try catch (SQLException e)
}
}
// public static void main (String[] args){
//
// BaseDao bo= new BaseDao();
// Connection conn=null;
// conn=bo.getConnection();
// System.out.print(conn);
//
// }
}
⑶ JSP編程題目求答案
publicclassRectangle{
privateIntegerrLength;//長
privateIntegerrWidth;//寬
privateIntegerrArea;//面積
publicIntegergetrLength(){
returnrLength;
}
publicvoidsetrLength(IntegerrLength){
this.rLength=rLength;
}
publicIntegergetrWidth(){
returnrWidth;
}
publicvoidsetrWidth(IntegerrWidth){
this.rWidth=rWidth;
}
publicIntegergetrArea(){
returnrArea;
}
publicvoidsetrArea(IntegerrArea){
this.rArea=rArea;
}
}
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtml>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<metaname="renderer"content="webkit|ie-comp|ie-stand">
<metahttp-equiv="X-UA-Compatible"content="IE=edge,chrome=1">
<metaname="viewport"
content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<metahttp-equiv="Cache-Control"content="no-siteapp"/>
<scripttype="text/javascript"src="jquery/1.9.1/jquery.min.js"></script>
<title></title>
</head>
<body>
<div>
<formaction="/xxxxxxxx"method="post"id="form-depart-edit">
<div>
<label><span>*</span>長:</label>
<div>
<inputtype="text"class="input-text"value=""placeholder="長"id="rLength"name="rLength">
</div>
</div>
<div>
<label><span>*</span>寬:</label>
<div>
<inputtype="text"class="input-text"value=""placeholder="寬"id="rWidth"name="rWidth">
</div>
</div>
<div>
<label><span>*</span>面積:</label>
<div>
<inputtype="text"value=""placeholder="面積"id="rArea"name="rArea"readonly="readonly">
</div>
</div>
<div>
<buttononClick="jisuan();"type="button">提交</button>
</div>
</form>
</div>
<scripttype="text/javascript">
functionjisuan(){
varrLength=$("#rLength").val();
varrWidth=$("#rWidth").val();
if(rWidth==null||rWidth==""||rLength==null||rLength==""){
alert("必須填寫長、寬!");
returnfalse;
}
varrArea=rLength*rWidth;
$("#rArea").val(rArea);
$("#form-depart-edit").submit();
}
</script>
</body>
</html>
⑷ jsp簡單編程題,幫個忙吧
主頁
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<body>
<form name="form" method="session" action="get.jsp">
<table>
<tr>
<td>請輸入內容:</td>
<td><input type="text" name="a"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="b" value="提交"></td>
</table>
</form>
</body>
</html>
get.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<body>
<jsp:useBean name="A" class="wlb.l" />
<jsp:setProperty name="A" property="a" >
<a href="show.jsp">成功獲取表單信息</a>
</body>
</html>
show.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<body>
<jsp:useBean name="A" class="wlb.l" />
<jsp:getProperty name="A" property="a" >
</body>
</html>
bean
package wlb.l;
public class U {
private String a;
public String getA() {
return name;
}
public void setA(String a) {
this.a = a;
}
}
資料庫連接
1.) 這個方法是把連接放在bean里
package com.lbc.msg.;
import java.sql.*;
public class BaseDao {
public final static String Driver ="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //是連接資料庫sql2005
public final static String Url="jdbc:sqlserver://localhost:1433;DatabaseName=wlb";//wlb是資料庫名
public final static String userid="sa";//登錄資料庫的帳號
public final static String pwd="123456";//密碼
public Connection getConn(){
Connection conn=null;
try{
Class.forName(Driver);
conn = DriverManager.getConnection(Url,userid,pwd);
}catch(Exception e)
{
System.out.println("驅動載入失敗!");
e.printStackTrace();
}
return conn;
}
public void closeAll(Connection conn,PreparedStatement stmt,ResultSet rs){
if(conn!=null){
try{
stmt.close();
}catch(Exception e)
{
System.out.println("conn關閉發生失敗!");
e.printStackTrace();
}
}
}
public int executeSQl(String sql,String []getValue){
Connection conn=null;
PreparedStatement stmt=null;
int num=0;
try{
conn=getConn();
stmt=conn.prepareStatement(sql);
if(getValue!=null){
for(int i=0;i<getValue.length;i++){
stmt.setString(i+1,getValue[i]);
}
}
num=stmt.executeUpdate();
}catch(Exception e){
System.out.println("executeSQl方法異常!");
e.printStackTrace();
}
return num;
}
}
2.)
這個是在頁面上
<%@ page contentType="text/html; charset=GBK" import="java.sql.*" %>
<%
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//是連接資料庫sql2005
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=wlb";//wlb是資料庫名
String userid="sa",pwd="123456",sql = "";//登錄資料庫的帳號和密碼
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,userid,pwd);
sql = "select * from Title where username='" + request.getParameter("username") + "' and password='" + request.getParameter("password") + "'";//這里就是查詢的語句
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
session.setAttribute("Username",request.getParameter("username"));
response.sendRedirect("mian.jsp");
}
else
{
%>
<script language="javascript">
alert("用戶名或密碼錯誤");
window.history.go(-1);
</script>
<%
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}
%>
沒有調試 有錯誤在問我,sql連接是我自己的連接
⑸ JSP編程技術測試題
一 選擇題(每小題 分 共 分)
.當多個用戶請求同一個JSP頁面時 Tomcat伺服器為每個客戶啟動一個(B)
A)進程 ? B)線程 C)程序 D服務
.不是JSP運行必須的是(D)
A)操作系統
B)JavaJDK
C)支持Jsp的Web伺服器
D)資料庫
.下列說法哪一項是正確的(C)
A)Apache用於ASP技術所開發網站的伺服器
B)IIS用於CGI技術所開發網站的伺服器
C)Tomcat用於JSP技術所開發網站的伺服器
D)WebLogic用於PHP技術所開發網站的伺服器
.下面描述錯誤的是(C)
A)HTML文件必須由<>開頭 </>標記結束
B)文檔頭信息包含在<head>與</head>之間
C)在<head>和</head>之間可以包含<title>和<body>等信息
D)文檔體包含在<body>和</body>標記之間
.設置文檔體背景顏色的屬性是(C )
A)text? B)bgcolor ? C) background D) link
.( D)是標題標記
A)<p>標記 B)<br>標記 ? C)<hr>標記? D)<hn>
.超級鏈接是互聯網的靈魂 下面哪個是正確的鏈接標記( A )
A)<a? href=&# ; >String readLine()方法 該方法讀入一行文本 這里的 一行 指字元串以 或什麼做結尾 ()
A) ?B)f
C) ? D)p
.下列代碼中rs為查詢得到的結果集 代碼運行後表格的每一行有幾個單元格(B )
while(rs next()){
out print(&# ;<tr>&# ;);
out print(&# ;<td>&# ;+rs getString( )+&# ;</td>&# ;);
out print(&# ;<td>&# ;+rs getString( )+&# ;</td>&# ;);
out print(&# ;<td>&# ;+rs getString( )+&# ;</td>&# ;);
out print(&# ;<td>&# ;+rs getString(&# ;publish&# ;)+&# ;</td>&# ;);
out print(&# ;<td>&# ;+rs getFloat(&# ;price&# ;)+&# ;</td>&# ;);
out print(&# ;</tr>&# ;);
}
A) ? ?B) ? C) D)不確定
.下面是載入JDBC資料庫驅動的代碼片段
try{
Class forName(&# ;sun jdbc odbc JdbcOdbcDriver&# ;);
}
catch(ClassNotFoundException e){
out print(e);
}
該程序載入的是哪個驅動?()
A)JDBC ODBC橋連接驅動? ?B)部分Java編寫本地驅動
C)本地協議純Java驅動 ? D)網路純Java驅動
.下面Servlet的哪個方法用來為請求服務 在Servlet生命周期中 Servlet每被請求一次它就會被調用一次 ( A )
A)service() B)init()
C)doPost() D)destroy()
.閱讀下面代碼片段
RequestDispatcher dispatcher=request getRequestDispatcher(&# ;a jsp&# ;);
dispatcher forward(request response);
關於該段代碼的作用 下列敘述哪項是正確的?(B)
A)頁面重定向到a jsp頁面 B)將請求轉發到a jsp頁面
C)從a jsp定向到當前頁面 D)從a jsp轉發到當前頁面
<h >二 判斷題(每小題 分 共 分) </h >.Internet和intranet的含義意義相同 ( 錯 )
.Web開發技術包括客戶端和伺服器端的技術 (對 )
.在網頁中圖形文件與網頁文件是分別存儲的 (對)
.超級鏈接不僅可以將文本作為鏈接對象 也可以將圖像作為鏈接對象 (對)
.盒子模型中的top right bottom left屬性取值即可以是像素數 也可是百分比 並且只有父容器的position取值為非static值時才有效 (對)
.超級鏈接<a>標記的target屬性取值為鏈接的目標窗名 可以是parent blank self top (錯)
.JSP引擎執行位元組碼文件的主要人物之一是直接將HTML內容發給客戶端 (對)
.JSP頁面中的指令標記 JSP動作標記統稱為腳本元素 (錯)
.在 <%! 和 %> 標記之間聲明的Java的方法在整個頁面內有效 (對)
.程序片變數的有效范圍與其聲明位置有關 即從聲明位置向後有效 可以在聲明位置後的程序片 表達式中使用 (對)
.不可以用一個page指令指定多個屬性的取值 (錯)
.jsp:param動作標記不能單獨使用 必須作為jsp:include jsp:forward標記等的子標記使用 並為它們提供參數 (對)
.Tomcat伺服器支持直接使用application對象 (錯)
.利用response對象的sendRedirect方法只能實現本網站內的頁面跳轉 但不能傳遞參數 (錯)
.contentType屬性用來設置JSP頁面的MIME類型和字元編碼集 取值格式為&# ;MIME類型&# ;或&# ;MIME類型;charset=字元編碼集&# ; response對象調用addHeader方法修改該屬性的值 (錯)
.response對象的_sendRedirect(URL url)___方法可以將當前客戶端的請求轉到其他頁面去
.JavaBean的屬性必須聲明為private 方法必須聲明為public訪問類型 (對)
.RandomAccessFile類即不是輸入流類InputSteam的子類 也不是輸出流類OutputStream的子類 但它同時實現了DataInput和DataOutput介面 (對)
.在MVC模式的Web開發中 視圖 模型 和 控制器 分別對應著 JSP頁面 JavaBean 和 Serlvet 以Servlet為核心 (對)
.doGet()和doPost()方法分別處理客戶端GET和POST方法發送的請求 (對)
<h >三 填空題(每空 分 共 分) </h >.W C是指___萬維網聯盟_______
.當今比較流行的技術研發模式是__C/S__和__B/S__的體系結構來實現的
.HTML文件是純文本文件格式 可以用文本編輯器進行編輯製作
.將一個圖像作為一個超級鏈接 用到了__<img>__標記
.URL是Uniform Resource Locator的縮寫 中文稱之為 統一資源定位器?
.JSP頁面的基本構成元素 其中變數和方法聲明(Declaration) 表達式(Expression)和Java程序片(Scriptlet)統稱為JSP腳本元素
. <%! 和 %> 之間聲明的方法在整個頁面內有效 稱為頁面的成員方法?
.JSP頁面中 輸出型注釋的內容寫在? <!&# ; ? 和? &# ;> ? 之間
.Page指令的屬性Language的默認值是_Java____
.<select>標記中的size屬性默認值為___ ___
.表單的提交方法包括___post_______和____get______方法
.out對象的__ out flush()_____方法 功能是輸出緩沖的內容
.使用Bean首先要在JSP頁面中使用 ? import ? 指令將Bean引入
.創建JavaBean的過程和編寫Java類的過程基本相似 可以在任何Java的編程環境下完成 編寫 編譯和發布
.response對象的 setContentType()? 用來定義伺服器發送給客戶端的MIME類型
.Servlet中使用Session對象的步驟為 調用 HttpServletRequest getSession() 得到Session對象 查看Session對象 在會話中保存數據
.MVC(Model View Controller)是八十年代為編程語言 Smalltalk 發明的一種軟體設計模式
.控制器主要負責兩方面的動作 ? 把用戶的請求分發到相應的模型? ? 將模型的改變及時反映到視圖上?
.在DOM介面規范中 有四個基本介面 __Node__ __Nodelist___ _NamedNodeMap__和_Document__
<h >四 簡答題(每小題 分 共 分) </h >.什麼是B/S模式?
.如何保證頁面跳轉時當前頁面與跳轉頁面之間的聯系?
.在<%!和%>之間聲明的變數和在<%和%>之間聲明的變數有何區別?
.如何在頁面的程序片中使用Bean?
lishixin/Article/program/Java/hx/201405/30804
⑹ JSP的編程題
呵呵應該是這樣寫的吧<%@ page language="java"%>
<html>
<head>
<title>Hellow Would!</title>
</head>
<body>
<%String msg="JSP Example";
out.println("Hellow Would!");%><%=msg%>
</body>
</html> 結果 這是java的小腳本<%java 代碼%>在裡面寫java的代碼<%=java表達式%>在裡面寫表達式就可以顯示到頁面了!!
⑺ 編程題:在Jsp中進行條件判斷以及相應的頁面跳轉 掌握最基本的JavaScript客戶端校驗
你可以寫個登錄校驗a.jsp,通過javascript判定用戶名和密碼,正確跳轉成功歡迎頁面b.jsp,失敗則跳轉到c.jsp頁面。
關鍵提示:
1.a.jsp form表單 提交按鈕設置onclick="check()"時間
2.腳本里獲取用戶名和密碼內容進行靜態判斷(a=="name" && b="password",頁面跳轉使用
window.open("url","_new");
3.b.jsp 輸出提示 登陸成功 c.jsp提示登錄失敗
4.
⑻ JSP 編程題 希望知道的人能提供正確答案!
3、Class.forName(「com.msyql.jdbc.Driver」);
Connection conn = DriverManager.getConnetion("jdbc:mysql://localhost:3306/test","root","root");
Statement stat = conn.createStatement();
ResultSet ts = stat.createQuery("select * from yuangong");
while(rs.next()){
ts.getString(0);
ts.getString(1);
}