本文目錄一覽:
- 1、做的jsp註冊界面,註冊的時候彈出下面代碼,請問是什麼原因,在線等
- 2、如何在jsp頁面上實現點擊註冊按鈕,彈出一個窗體來註冊(類似於百度貼吧的登錄和註冊),求詳細代碼和注釋
- 3、求大神寫一下jsp的簡單的註冊界面代碼。
- 4、idea 運行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-tw/n/183912.html