发布网友 发布时间:2022-04-23 01:48
共2个回答
热心网友 时间:2023-04-27 02:46
你的传值方法用反了,你上面的<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..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 user=new UserDao();
try {
flag=user.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业务处理类User来处理
package com.icss.;
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();
}
}
}
呼呼,写这个真是有点浪费时间~·不过好歹完工了,当作是复习一下。希望对你有用。。
追问你这个同next关键字没有关联,不过还是要谢谢你!
热心网友 时间:2023-04-27 02:46
java哪有next关键字啊