jsp註冊內容顯示代碼(jsp實現註冊)

本文目錄一覽:

做的jsp註冊界面,註冊的時候彈出下面代碼,請問是什麼原因,在線等

①可能是你的表單裡面某個文本框沒有填東西(可以添加表單驗證,避免必填項未填值);

②然後你又在RegisterServlet中通過request.getParameter(“XXX”)的方式引用了這個文本框中的值(當然它為null);

③ 接下來你又把這個引用的值賦給了某個變量String var;

④ 最後把這個變量var傳給了某個方法作為實參。

所以就發生了實參的值為Null的空指針異常。

如何在jsp頁面上實現點擊註冊按鈕,彈出一個窗體來註冊(類似於百度貼吧的登錄和註冊),求詳細代碼和注釋

jsp中的註冊彈出新窗口是通過window.open一個新頁面來實現的。

頁面register.jsp代碼如下:

%@ page contentType=”text/html; charset=gb2312″ language=”java” import=”cn.wy.Pet.User” errorPage=”” %

!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”

html xmlns=””

head

meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ /

title會員註冊例子講解/title

style type=”text/css”

!–

.STYLE1 {

color: #FF0000;

font-weight: bold;

}

.STYLE2 {color: #FF0000}

.STYLE3 {

font-size: 18px;

font-weight: bold;

}

/style

/head

body style=”font-size:12px”

form id=”form1″ name=”form1″ method=”post” action=”%=actionStr%reg”

p align=”center”br /

span class=”STYLE3″用戶註冊/span/p

table width=”582″ height=”302″ border=”1″ align=”center” cellpadding=”0″ cellspacing=”0″ bordercolor=”#BCACD2″

tr

td width=”80″ align=”right”用戶名:/td

td width=”496″ align=”left”input name=”userName” type=”text” id=”userName” size=”16″ maxlength=”16″ /

span class=”STYLE1″*/span 3~16位字母或者數字(如:8hack)/td

/tr

tr

td align=”right”密碼:/td

td align=”left”input name=”password1″ type=”text” id=”password1″ size=”16″ maxlength=”16″ /

span class=”STYLE1″* /span 3~16位字母或者數字(如:abc123)/td

/tr

tr

td align=”right”確認密碼:/td

td align=”left”input name=”password2″ type=”text” id=”password2″ size=”16″ maxlength=”16″ /

span class=”STYLE1″*/span 必須和上面輸入的密碼相同/td

/tr

tr

td align=”right”電子郵件:/td

td align=”left”input name=”email” type=”text” id=”email” maxlength=”20″ /

span class=”STYLE1″*/span 找回密碼和聯繫用(如:8hack@163.com)/td

/tr

tr

td align=”right”聯繫電話:/td

td align=”left”input name=”tel” type=”text” id=”tel” size=”20″ maxlength=”20″ /

如(0871-8888888,13888853113)/td

/tr

tr

td align=”right”聯繫地址:/td

td align=”left”input name=”address” type=”text” id=”address” maxlength=”50″ //td

/tr

td height=”40″ colspan=”2″ align=”center”input type=”submit” name=”Submit” value=”確認註冊” /

input type=”reset” name=”Submit2″ value=”重新填寫” //td

/tr

/table

/form

/body

/html

後台servlet的處理:

public class reg extends HttpServlet

{

public reg()

{

}

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

PrintWriter out;

DBConnection dbc=null;

String userName;

String psd;

String email;

String tel;

String address;

int popedom;

response.setContentType(“text/html;charset=UTF-8”);

out = response.getWriter();

try{

dbc = new DBConnection();

PreparedStatement ps = null;

userName = request.getParameter(“userName”);

psd = login.encrypt(request.getParameter(“password1”).toString());

email = request.getParameter(“email”);

tel = request.getParameter(“tel”);

address = request.getParameter(“address”);

popedom = Integer.parseInt(request.getParameter(“popedom”));

if (userName != null psd != null email != null)

{

ps = dbc.getCon().prepareStatement(“insert into [User](UName,Upass,UEmail,UTel,UAddress,UPopedom) values(?,?,?,?,?,?)”);

ps.setString(1, userName);

ps.setString(2, psd);

ps.setString(3, email);

ps.setString(4, tel);

ps.setString(5, address);

ps.setInt(6, popedom);

ps.execute();

System.out.print(“新用戶註冊:” + request.getParameter(“userName”) + ” “);

out.print(“scriptalert(‘恭喜您:註冊成功!現已經登錄到網站!’);history.go(-1)/script”);

}

if (dbc != null)

dbc.dbClose();

}

catch(SQLException ex)

{

out.print(“scriptalert(‘註冊失敗!數據庫連接錯誤!’);history.go(-1)/script”);

ex.printStackTrace();

if (dbc != null)

dbc.dbClose();

}

}

}

求大神寫一下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;

}

}

idea 運行JSP後顯示源代碼是什麼情況

1.這種情況,應該是jsp的內容被當做文本直接顯示到了頁面上,一般在使用springMVC時可能出現這樣的問題,猜測可能使用了springMVC。

2.具體解決方案:

查找web.xml文件,並找到springMVC的相關配置

servlet-mapping

servlet-namespringMVC/servlet-name

url-pattern/*/url-pattern

/servlet-mapping

將上面的內容改為下面的即可 攔截是/ 而不是/*

servlet-mapping

servlet-namespringMVC/servlet-name

url-pattern//url-pattern

/servlet-mapping

原因:在這種情況向springMVC會把*.jsp,*.sql,*.txt都當做txt處理。結果就是直接在瀏覽器加載了jsp源碼。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/183912.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-25 05:49
下一篇 2024-11-25 05:49

相關推薦

  • 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
  • 北化教務管理系統介紹及開發代碼示例

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

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

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

    編程 2025-04-29

發表回復

登錄後才能評論