jsp全部代碼,JSP編程

本文目錄一覽:

jsp程序代碼

%@ page language=”java” import=”java.util.*” errorPage=”error.jsp” pageEncoding=”UTF-8″%

% request.setCharacterEncoding(“GBK”);%

html

head

title動作處理頁面/title

/head

body background=”C:\tp\yy.JPG”

font color=yellow

centerbrh2

jsp:useBean id=”loginbean” class=”com.login.LoginBean”/jsp:useBean //使用jsp:useBean標籤調用LoginBean.java,class指明JavaBean位置

jsp:setProperty name=”loginbean” property=”*” ///設置LoginBean的屬性值

%out.println(“用戶號為:”+loginbean.getUserid()+”brbr”); 輸出loginbean裡面getUserid的值

if (loginbean.getUserid()==null){ //如果用戶名為空

throw new Exception(“尊敬的用戶:系統登錄頁面中的用戶名不能為空,請輸入用戶號!”);

}

if (loginbean.getPsw()==null){ //如果密碼為空

throw new Exception(“尊敬的用戶:系統登錄頁面中的密碼不能為空,請輸入密碼!”);

}

if (loginbean.getPsw().equalsIgnoreCase(“neusoft”)) //如果JavaBean的密碼和輸入密碼相同

{

String puserid = request.getParameter(“userid”); //獲得此用戶ID

Cookie cookies[] = request.getCookies();

//如果是上一步提交過來的,則更新Cookie的值

if(puserid != null) {

Cookie c = new Cookie(“userid”, puserid);

c.setMaxAge(60*60*24*30); //Cookie的有效期為1個月setMaxAge()是Cookie 的一個方法

器單位為秒

60*60*24*30等與30天*24小時*60分*60秒==結果就為1個月的秒數

response.addCookie(c);

}

session.setAttribute(“login”,”ok”);

session.setMaxInactiveInterval(-1);

%

jsp:forward page=”success.jsp” / //跳轉至success.jsp

%

}

else { //如果密碼不正確則執行

out.println(“對不起,密碼輸入錯誤!您此次系統登陸失敗!請重新輸入密碼!brbr”);

out.println(“A href=login.jsp返回/Abr”);

}

%

h2

br

hr

/font

/body

/html

答題完畢

解釋JSP代碼,越詳細越好

%

Condb con=new Condb(); //建立資料庫連接

int num=0; 定義計數變數 初始化為0

String sql=”select * from tb_Result where Result='”+1+”‘”; 資料庫查詢命令

ResultSet rs=con.executeQuery(sql); 執行資料庫查詢命令

while(rs.next()){ 判斷是否是否是記錄集最後一條同時移動資料庫記錄至下一條

num++; 計數變數自加1

%

tr

td width=”37″ height=”22″ align=”center”%=num%/td

td height=”22″ align=”left”a href=”Rparticular.jsp?temp=%=rs.getString(2)%” 得到資料庫第三個欄位的值作為temp參數的值

%String str=rs.getString(2);out.println(str);% 輸出資料庫第三個欄位的值

/a/td

td height=”22″ align=”center”%=rs.getDate(8)%/td

td height=”22″ align=”center”%=rs.getString(9)%/td

/tr

%

}

con.close(); 關閉資料庫連接

%

編寫用戶註冊於登錄的JSP頁面的全部程序代碼

3個jsp文件,第一個是login.jsp,第二個是judge.jsp,第三個是afterLogin.jsp

%@ page language=”java” contentType=”text/html; charset=GB18030″

pageEncoding=”GB18030″%

%@ page import=”java.util.*” %

!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”

html

head

title登錄頁面/title

/head

body

form name=”loginForm” method=”post” action=”judgeUser.jsp”

table

tr

td用戶名:input type=”text” name=”userName” id=”userName”/td

/tr

tr

td密碼:input type=”password” name=”password” id=”password”/td

/tr

tr

tdinput type=”submit” value=”登錄” style=”background-color:pink” input type=”reset” value=”重置” style=”background-color:red”/td

/tr

/table

/form

/body

/html

%@ page language=”java” contentType=”text/html; charset=GB18030″

pageEncoding=”GB18030″%

%@ page import=”java.util.*” %

!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”

html

head

title身份驗證/title

/head

body

%

request.setCharacterEncoding(“GB18030”);

String name = request.getParameter(“userName”);

String password = request.getParameter(“password”);

if(name.equals(“abc”) password.equals(“123”)) {

%

jsp:forward page=”afterLogin.jsp”

jsp:param name=”userName” value=”%=name%”/

/jsp:forward

%

}

else {

%

jsp:forward page=”login.jsp”/

%

}

%

/body

/html

%@ page language=”java” contentType=”text/html; charset=GB18030″

pageEncoding=”GB18030″%

!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”

html

head

title登錄成功/title

/head

body

%

request.setCharacterEncoding(“GB18030”);

String name = request.getParameter(“userName”);

out.println(“歡迎你:” + name);

%

/body

/html

求大神寫一下jsp的簡單的註冊界面代碼。

1.需要一個jsp頁面:

//login.jsp核心代碼:

form action=”${pageContext.request.contextPath}/servlet/UserServlet” method=”post”

input type=”text” name=”loginname” /input type=”password” name=”password”/

input type=”submit” value=”登錄”/

/form

2.需要一個servlet來驗證登錄信息

//UserServlet 核心代碼

class UserServlet extends HttpServlet{

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

process(request, response);

}

protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

process(request, response);

}

private void process(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

PrintWriter pw = response.getWriter();

request.setCharacterEncoding(“UTF-8”);

response.setContentType(“text/html”);

String loginname = request.getParameter(“loginname”);

String password = request.getParameter(“password”);

//創建一個service來處理業務邏輯(包括查詢資料庫操作)

UserService service = new UserService();

boolean bool = service.validateUser(loginname,password);

if(!bool){

pw.println(“用戶名或密碼錯誤”);

}else{

pw.println(“登錄成功”);

}

}

3.需要一個service處理業務邏輯(包括查詢資料庫操作)

//UserService 核心代碼

public class UserService{

/**

*查詢資料庫驗證用戶是否存在,返回boolean

*/

public boolean validateUser(String loginname,String password){

boolean bool = false;

Connection conn = null;

PreparedStatement ps = null;

//這裡以mysql為例

try {

Class.forName(“com.mysql.jdbc.Driver”).newInstance();

conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”, “root”, “”);

String sql = “select login_name,pass_word from t_user where login_name=? and pass_word=?”;

ps = conn.prepareStatement(sql);

ps.setString(0, loginname);

ps.setString(1, password);

ResultSet rs = ps.executeQuery();

if(rs.next()){

bool = true;

}

} catch (Exception e) {

e.printStackTrace();

} finally{

try {

if(conn != null){

conn.close();

conn = null;

}

if(ps != null){

ps.close();

ps = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

return bool;

}

}

原創文章,作者:QGIA,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/134402.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
QGIA的頭像QGIA
上一篇 2024-10-04 00:05
下一篇 2024-10-04 00:05

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29
  • Python愛心代碼動態

    本文將從多個方面詳細闡述Python愛心代碼動態,包括實現基本原理、應用場景、代碼示例等。 一、實現基本原理 Python愛心代碼動態使用turtle模塊實現。在繪製一個心形的基礎…

    編程 2025-04-29
  • 北化教務管理系統介紹及開發代碼示例

    本文將從多個方面對北化教務管理系統進行介紹及開發代碼示例,幫助開發者更好地理解和應用該系統。 一、項目介紹 北化教務管理系統是一款針對高校學生和教職工的綜合信息管理系統。系統實現的…

    編程 2025-04-29

發表回復

登錄後才能評論