求助:用JSP写个网页

落雪儿 2011-05-15 06:17:33
用JSP写个页面,能在浏览器上访问,再在页面上加上能访问数据库的语句。。。。我不太懂。。。希望高手能够帮帮忙
...全文
99 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
有点异样 2011-05-18
  • 打赏
  • 举报
回复
register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%
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>register!</title>
</head>

<body>
<form action="registerDeal.jsp">
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name="password"><br>
重复密码:<input type="password" name="password2"><br>
<input type="submit" value="提交">
</form>
</body>
</html>


registerDeal.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="java.sql.*" %>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
String password2=request.getParameter("password2");

Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/register","root","12345");
String sqlQuery="select count(*) from user where username=?";
PreparedStatement psQuery=conn.prepareStatement(sqlQuery);
psQuery.setString(1,username);
ResultSet rs=psQuery.executeQuery();
rs.next();
int count=rs.getInt(1);
if(count>0){
rs.close();psQuery.close();conn.close();
response.sendRedirect("registerFail.jsp");
return;
}

String sql="insert into user values(null,?,?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
ps.executeUpdate();
ps.close();conn.close();
response.sendRedirect("registerSuccess.jsp");
%>


registerSuccess.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%
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>register!</title>
</head>

<body>
Success! <br>
</body>
</html>

registerFail.jsp
[code=Java]
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%
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>register!</title>
</head>

<body>
Fail!<br>
</body>
</html>


[/code]
刘朝雷 2011-05-18
  • 打赏
  • 举报
回复
你说下你具体的需求呗
我现在是学JSP的还可以帮你解决

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧