本文目录一览:
- 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源码。