首先第一步:
我们需要建立数据库,在数据库中建立users表,字段分别为id(主键) username(账号) password(密码) roleID(角色),这个不做详细解释。
接下来我们需要建立jsp页面,在这里建立3个jsp页面,分别为index.jsp(插入数据页面)、fuction.jsp(执行插入数据的逻辑操作)、success.jsp(跳转成功页面)。PS:其实还可以建立一个插入失败时跳转的页面,自己可以试试。
下面是关键代码:
index.jsp中主要是几个文本框用来填写数据:
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="This is my page">
<!--www.mwcly.cn
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<formaction="fuction.jsp"method="post">
<tablewidth="400"align="center"border="1"cellpadding="0"cellspacing="0">
<tr><tdcolspan="2"align="center">向数据库插入信息测试</td></tr>