
我想在Java里面实现提交的功能,不用Struts如何实现呢?
比如:前台:<%@pagelanguage="java"import="java.util.*"pageEncoding="gbk"%><%Stringpath=requ...
比如:
前台:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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>
<script type="text/javascript">
var i = 0;
function uploadFile() {
document.getElementById('upload'+i).innerHTML = "<input type='FILE' name='FILE1' size='30'><div id=upload"+(i+1)+"></div>";
i++;
}
</script>
</head>
<body>
<p> </p>
<p align="center">上传文件选择</p>
<FORM METHOD="POST"
ACTION="<%=basePath%>upload"
enctype="multipart/form-data">
<p>
<label>
*资源名称:</label>
<input id="txtTitle" name="txtTitle" tabindex="1" style="width: 380px;" value="" class="textbox" type="text" maxlength="250">
<br/><label></label>需要大于10个字符
</p>
<p>
<label>
*关键字(Tag):</label>
<input id="txtTag" name="txtTag" tabindex="2" style="width: 380px;" value="" class="textbox" type="text" maxlength="100">
<br/><label></label>多个关键字请用空格分隔,最多填写5个。点击右侧Tag快速添加
</p>
<table width="75%" border="1" align="center">
<tbody >
<tr>
<td>
<input type="FILE" name="FILE1" size="30">
</td>
</tr>
</tbody>
<tr>
<td>
<a href="javascript:uploadFile()">继续添加</a>
<input type="submit" name="Submit" value="上传">
</td>
</tr>
</table>
</FORM>
</body>
</html>
比如上面的txtTitle,txtTag,在后台如何取到呢。
谢谢各位大侠的帮助,一楼的同志,我用了你的方法,可还是得不到结果啊!
前台是:
<FORM METHOD="POST" ACTION="<%=basePath%>upload" enctype="multipart/form-data">
<input id="txtTitle" name="txtTitle" tabindex="2" style="width: 380px;" value="" class="textbox" type="text" maxlength="100">
<input type="FILE" name="FILE1" size="30">
<input type="submit" name="Submit" value="上传">
</FORM>
后台:
最基本的JSP+Servlet技术
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setContentType("text/html");
String txtTitle = request.getParameter("txtTitle");//获取txtTitle的信息
String txtTag = request.getParameter("txtTag");//获取txtTag的信息
}
二楼的同志,我是菜鸟,刚开始学习Java,能说的详细一点吗? 展开
前台:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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>
<script type="text/javascript">
var i = 0;
function uploadFile() {
document.getElementById('upload'+i).innerHTML = "<input type='FILE' name='FILE1' size='30'><div id=upload"+(i+1)+"></div>";
i++;
}
</script>
</head>
<body>
<p> </p>
<p align="center">上传文件选择</p>
<FORM METHOD="POST"
ACTION="<%=basePath%>upload"
enctype="multipart/form-data">
<p>
<label>
*资源名称:</label>
<input id="txtTitle" name="txtTitle" tabindex="1" style="width: 380px;" value="" class="textbox" type="text" maxlength="250">
<br/><label></label>需要大于10个字符
</p>
<p>
<label>
*关键字(Tag):</label>
<input id="txtTag" name="txtTag" tabindex="2" style="width: 380px;" value="" class="textbox" type="text" maxlength="100">
<br/><label></label>多个关键字请用空格分隔,最多填写5个。点击右侧Tag快速添加
</p>
<table width="75%" border="1" align="center">
<tbody >
<tr>
<td>
<input type="FILE" name="FILE1" size="30">
</td>
</tr>
</tbody>
<tr>
<td>
<a href="javascript:uploadFile()">继续添加</a>
<input type="submit" name="Submit" value="上传">
</td>
</tr>
</table>
</FORM>
</body>
</html>
比如上面的txtTitle,txtTag,在后台如何取到呢。
谢谢各位大侠的帮助,一楼的同志,我用了你的方法,可还是得不到结果啊!
前台是:
<FORM METHOD="POST" ACTION="<%=basePath%>upload" enctype="multipart/form-data">
<input id="txtTitle" name="txtTitle" tabindex="2" style="width: 380px;" value="" class="textbox" type="text" maxlength="100">
<input type="FILE" name="FILE1" size="30">
<input type="submit" name="Submit" value="上传">
</FORM>
后台:
最基本的JSP+Servlet技术
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setContentType("text/html");
String txtTitle = request.getParameter("txtTitle");//获取txtTitle的信息
String txtTag = request.getParameter("txtTag");//获取txtTag的信息
}
二楼的同志,我是菜鸟,刚开始学习Java,能说的详细一点吗? 展开
4个回答
展开全部
你的传值方法用反了,你上面的<form>表单里面写的方法是post但是你在后台Servlet处理的时候用的是doGet方法处理,是不可能接受到数据的。做这个上传东西的话,需要导入一些外包,像cosFir这种包来实现比较好一点。
如果只是实现提交功能的话,首先思路要明确,你表单的内容提交过来,要直接获取的话用你的方法就可以了,但是注意传值的方法,一般用post传值,因为get方法传值太小,容易丢失数据。还有就是你传过来了在servlet处理的时候最后弄个标记来处理,这样可以利用if分支来处理多个页面出来的的请求,当然servlet也只是初步的处理,你最后还得调用javabean来处理,就是你要用一个业务类来专门处理你的sql语句接串,然后提交数据可处理。看一下我写的一个登录的小程序:
登录页面:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("gbk");
String rs_flag=(String)request.getAttribute("rs_flag");
if(rs_flag!=null){
out.println(rs_flag);
}
%>
请登陆:
<form action="LoginServlet" name="form1" method="post">
用户名:<input type="text" name="consumername" >
密码:<input type="password" name="consumerpsw">
<input type="submit" name="sub" value="登陆">
</form>
</body>
</html>
servlet处理Loginservlet
package com.icss.contr;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.icss.dao.UserDao;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* servlet要完成用户提交信息的整理
*
*/
request.setCharacterEncoding("GBK");
String consumername=request.getParameter("consumername");
String consumerpsw=request.getParameter("consumerpsw");
int flag=0;
/*
* servlet要控制请求由那个业务类处理
*/
UserDao userdao=new UserDao();
try {
flag=userdao.validate(consumername, consumerpsw);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* servlet 要根据业务类处理的结果(判断分支)选择响应的jsp页面
*/
if(flag==0){
/*
* 在响应之前在做显示信息的准备
*/
String rs_flag="登陆失败!请重试!";
request.setAttribute("rs_flag",rs_flag);
//转发
request.getRequestDispatcher("index.jsp").forward(request,response);
}else{
request.getRequestDispatcher("main.jsp").forward(request,response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
然后调用javabean业务处理类Userdao来处理
package com.icss.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.icss.base.DbCon;
import com.icss.vo.UserVo;
public class UserDao extends DbCon {
public int validate(String consumername,String consumerpsw) throws ClassNotFoundException, SQLException{
getCon();
String sql="select * from consumer where consumername='"+consumername+"' and consumerpsw='"+consumerpsw+"'" ;
int flag=0;
ResultSet rs=DbCon.st.executeQuery(sql);
if(rs.next()){
flag=1;
}
rs.close();
closeCon();
return flag;
}
}
处理完数据库的接串以及查询以后返回结果到servlet做处理,然后返回jsp页面就可以了。
下面这个是连接数据库的,我不知道你用什么数据库,我用的是oracle,不过都差不多,你看一下把:
package com.icss.base;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DbCon {
public static Connection conn=null;
public static Statement st=null;
public void getCon() throws ClassNotFoundException, SQLException{
Class.forName("oracle.jdbc.driver.OracleDriver");//创建数据库驱动对象
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.207:1521:ICSS","TEST","MANAGER");//创建数据库连接对象
st=conn.createStatement();//创建执行sql语句对象
}
public void closeCon() throws SQLException{
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
}
/**
* @param args
*/
public static void main(String[] args) {
DbCon dbc=new DbCon();
try {
dbc.getCon();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
呼呼,写这个真是有点浪费时间~·不过好歹完工了,当作是复习一下。希望对你有用。。
如果只是实现提交功能的话,首先思路要明确,你表单的内容提交过来,要直接获取的话用你的方法就可以了,但是注意传值的方法,一般用post传值,因为get方法传值太小,容易丢失数据。还有就是你传过来了在servlet处理的时候最后弄个标记来处理,这样可以利用if分支来处理多个页面出来的的请求,当然servlet也只是初步的处理,你最后还得调用javabean来处理,就是你要用一个业务类来专门处理你的sql语句接串,然后提交数据可处理。看一下我写的一个登录的小程序:
登录页面:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("gbk");
String rs_flag=(String)request.getAttribute("rs_flag");
if(rs_flag!=null){
out.println(rs_flag);
}
%>
请登陆:
<form action="LoginServlet" name="form1" method="post">
用户名:<input type="text" name="consumername" >
密码:<input type="password" name="consumerpsw">
<input type="submit" name="sub" value="登陆">
</form>
</body>
</html>
servlet处理Loginservlet
package com.icss.contr;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.icss.dao.UserDao;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* servlet要完成用户提交信息的整理
*
*/
request.setCharacterEncoding("GBK");
String consumername=request.getParameter("consumername");
String consumerpsw=request.getParameter("consumerpsw");
int flag=0;
/*
* servlet要控制请求由那个业务类处理
*/
UserDao userdao=new UserDao();
try {
flag=userdao.validate(consumername, consumerpsw);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* servlet 要根据业务类处理的结果(判断分支)选择响应的jsp页面
*/
if(flag==0){
/*
* 在响应之前在做显示信息的准备
*/
String rs_flag="登陆失败!请重试!";
request.setAttribute("rs_flag",rs_flag);
//转发
request.getRequestDispatcher("index.jsp").forward(request,response);
}else{
request.getRequestDispatcher("main.jsp").forward(request,response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
然后调用javabean业务处理类Userdao来处理
package com.icss.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.icss.base.DbCon;
import com.icss.vo.UserVo;
public class UserDao extends DbCon {
public int validate(String consumername,String consumerpsw) throws ClassNotFoundException, SQLException{
getCon();
String sql="select * from consumer where consumername='"+consumername+"' and consumerpsw='"+consumerpsw+"'" ;
int flag=0;
ResultSet rs=DbCon.st.executeQuery(sql);
if(rs.next()){
flag=1;
}
rs.close();
closeCon();
return flag;
}
}
处理完数据库的接串以及查询以后返回结果到servlet做处理,然后返回jsp页面就可以了。
下面这个是连接数据库的,我不知道你用什么数据库,我用的是oracle,不过都差不多,你看一下把:
package com.icss.base;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DbCon {
public static Connection conn=null;
public static Statement st=null;
public void getCon() throws ClassNotFoundException, SQLException{
Class.forName("oracle.jdbc.driver.OracleDriver");//创建数据库驱动对象
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.207:1521:ICSS","TEST","MANAGER");//创建数据库连接对象
st=conn.createStatement();//创建执行sql语句对象
}
public void closeCon() throws SQLException{
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
}
/**
* @param args
*/
public static void main(String[] args) {
DbCon dbc=new DbCon();
try {
dbc.getCon();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
呼呼,写这个真是有点浪费时间~·不过好歹完工了,当作是复习一下。希望对你有用。。
展开全部
最基本的JSP+Servlet技术
package simplesession;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class simpleExample extends HttpServlet {
public simpleExample() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setContentType("text/html");
String txtTitle = request.getParameter("txtTitle");//获取txtTitle的信息
String txtTag = request.getParameter("txtTag");//获取txtTag的信息
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
这是我写的一个实例,希望对你有帮助
package simplesession;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class simpleExample extends HttpServlet {
public simpleExample() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setContentType("text/html");
String txtTitle = request.getParameter("txtTitle");//获取txtTitle的信息
String txtTag = request.getParameter("txtTag");//获取txtTag的信息
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
这是我写的一个实例,希望对你有帮助
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这种包含图片上传的,,直接用servlet需获
取到请求上下文 要截取字符串 然后要解码 很复杂
最好用上传组件 Apache 的commons 里的FileUpload 可以做 struts用的也是它
取到请求上下文 要截取字符串 然后要解码 很复杂
最好用上传组件 Apache 的commons 里的FileUpload 可以做 struts用的也是它
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的传值方法用反了,你上面的<form>表单里面写的方法是post但是你在后台Servlet处理的时候用的是doGet方法处理,是不可能接受到数据的。做这个上传东西的话,需要导入一些外包,像cosFir这种包来实现比较好一点。
如果只是实现提交功能的话,首先思路要明确,你表单的内容提交过来,要直接获取的话用你的方法就可以了,但是注意传值的方法,一般用post传值,因为get方法传值太小,容易丢失数据。还有就是你传过来了在servlet处理的时候最后弄个标记来处理,这样可以利用if分支来处理多个页面出来的的请求,当然servlet也只是初步的处理,你最后还得调用javabean来处理,就是你要用一个业务类来专门处理你的sql语句接串,然后提交数据可处理。看一下我写的一个登录的小程序:
登录页面:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("gbk");
String rs_flag=(String)request.getAttribute("rs_flag");
if(rs_flag!=null){
out.println(rs_flag);
}
%>
请登陆:
<form action="LoginServlet" name="form1" method="post">
用户名:<input type="text" name="consumername" >
密码:<input type="password" name="consumerpsw">
<input type="submit" name="sub" value="登陆">
</form>
</body>
</html>
servlet处理Loginservlet
package com.icss.contr;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.icss.dao.UserDao;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* servlet要完成用户提交信息的整理
*
*/
request.setCharacterEncoding("GBK");
String consumername=request.getParameter("consumername");
String consumerpsw=request.getParameter("consumerpsw");
int flag=0;
/*
* servlet要控制请求由那个业务类处理
*/
UserDao userdao=new UserDao();
try {
flag=userdao.validate(consumername, consumerpsw);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* servlet 要根据业务类处理的结果(判断分支)选择响应的jsp页面
*/
if(flag==0){
/*
* 在响应之前在做显示信息的准备
*/
String rs_flag="登陆失败!请重试!";
request.setAttribute("rs_flag",rs_flag);
//转发
request.getRequestDispatcher("index.jsp").forward(request,response);
}else{
request.getRequestDispatcher("main.jsp").forward(request,response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
然后调用javabean业务处理类Userdao来处理
package com.icss.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.icss.base.DbCon;
import com.icss.vo.UserVo;
public class UserDao extends DbCon {
public int validate(String consumername,String consumerpsw) throws ClassNotFoundException, SQLException{
getCon();
String sql="select * from consumer where consumername='"+consumername+"' and consumerpsw='"+consumerpsw+"'" ;
int flag=0;
ResultSet rs=DbCon.st.executeQuery(sql);
if(rs.next()){
flag=1;
}
rs.close();
closeCon();
return flag;
}
}
处理完数据库的接串以及查询以后返回结果到servlet做处理,然后返回jsp页面就可以了。
下面这个是连接数据库的,我不知道你用什么数据库,我用的是oracle,不过都差不多,你看一下把:
package com.icss.base;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DbCon {
public static Connection conn=null;
public static Statement st=null;
public void getCon() throws ClassNotFoundException, SQLException{
Class.forName("oracle.jdbc.driver.OracleDriver");//创建数据库驱动对象
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.207:1521:ICSS","TEST","MANAGER");//创建数据库连接对象
st=conn.createStatement();//创建执行sql语句对象
}
public void closeCon() throws SQLException{
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
}
/**
* @param args
*/
public static void main(String[] args) {
DbCon dbc=new DbCon();
try {
dbc.getCon();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
呼呼,写这个真是有点浪费时间~·不过好歹完工了,当作是复习一下。希望对你有用。。
如果只是实现提交功能的话,首先思路要明确,你表单的内容提交过来,要直接获取的话用你的方法就可以了,但是注意传值的方法,一般用post传值,因为get方法传值太小,容易丢失数据。还有就是你传过来了在servlet处理的时候最后弄个标记来处理,这样可以利用if分支来处理多个页面出来的的请求,当然servlet也只是初步的处理,你最后还得调用javabean来处理,就是你要用一个业务类来专门处理你的sql语句接串,然后提交数据可处理。看一下我写的一个登录的小程序:
登录页面:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("gbk");
String rs_flag=(String)request.getAttribute("rs_flag");
if(rs_flag!=null){
out.println(rs_flag);
}
%>
请登陆:
<form action="LoginServlet" name="form1" method="post">
用户名:<input type="text" name="consumername" >
密码:<input type="password" name="consumerpsw">
<input type="submit" name="sub" value="登陆">
</form>
</body>
</html>
servlet处理Loginservlet
package com.icss.contr;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.icss.dao.UserDao;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* servlet要完成用户提交信息的整理
*
*/
request.setCharacterEncoding("GBK");
String consumername=request.getParameter("consumername");
String consumerpsw=request.getParameter("consumerpsw");
int flag=0;
/*
* servlet要控制请求由那个业务类处理
*/
UserDao userdao=new UserDao();
try {
flag=userdao.validate(consumername, consumerpsw);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* servlet 要根据业务类处理的结果(判断分支)选择响应的jsp页面
*/
if(flag==0){
/*
* 在响应之前在做显示信息的准备
*/
String rs_flag="登陆失败!请重试!";
request.setAttribute("rs_flag",rs_flag);
//转发
request.getRequestDispatcher("index.jsp").forward(request,response);
}else{
request.getRequestDispatcher("main.jsp").forward(request,response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
然后调用javabean业务处理类Userdao来处理
package com.icss.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.icss.base.DbCon;
import com.icss.vo.UserVo;
public class UserDao extends DbCon {
public int validate(String consumername,String consumerpsw) throws ClassNotFoundException, SQLException{
getCon();
String sql="select * from consumer where consumername='"+consumername+"' and consumerpsw='"+consumerpsw+"'" ;
int flag=0;
ResultSet rs=DbCon.st.executeQuery(sql);
if(rs.next()){
flag=1;
}
rs.close();
closeCon();
return flag;
}
}
处理完数据库的接串以及查询以后返回结果到servlet做处理,然后返回jsp页面就可以了。
下面这个是连接数据库的,我不知道你用什么数据库,我用的是oracle,不过都差不多,你看一下把:
package com.icss.base;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DbCon {
public static Connection conn=null;
public static Statement st=null;
public void getCon() throws ClassNotFoundException, SQLException{
Class.forName("oracle.jdbc.driver.OracleDriver");//创建数据库驱动对象
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.207:1521:ICSS","TEST","MANAGER");//创建数据库连接对象
st=conn.createStatement();//创建执行sql语句对象
}
public void closeCon() throws SQLException{
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
}
/**
* @param args
*/
public static void main(String[] args) {
DbCon dbc=new DbCon();
try {
dbc.getCon();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
呼呼,写这个真是有点浪费时间~·不过好歹完工了,当作是复习一下。希望对你有用。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询