急急!!!!表单登录(高手请入)

zoukevin 2003-10-29 03:32:06
我用java servlet做了一个表单登陆,通过doGet的Form表单action='loginservlet' 进入loginservlet的doPost事件,在doPost通过与数据库的连接判断是否输入的用户帐号和密码正确,如正确进入mainservlet,这样做由于有 out.println("window.opener = null;");
out.println("window.close('loginservlet')");
out.println("window.open('mainservlet','','fullscreen=yes')");
这三句代码,显得很不连贯,现在我想直接在doGet的Form的action改成action='mainservlet' method='post'通过与数据连接判断进入mainservlet,而不通过loginservlet的doPost事件,请问怎么做呢, 同时我的数据库采用的是JDBC-ODBC桥连接,很慢,能不能帮我用别的连接方式改进呢,谢谢?
代码如下:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class LoginServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
public static String toChinese(String strvalue)
{
try{
if(strvalue==null)
return null;
else
{
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}catch(Exception e){
return null;
}
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession(true);
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title></title>");
out.println("<META http-equiv=Content-Type content='text/html; charset=gb2312'>");
out.println("</head>");
out.println("<BODY text=black bgColor=white leftMargin=1 MARGINWIDTH='1'><br>");
out.println("<CENTER><IMG alt='' src='file:///E:/wl/images/ball.jpg' width='584' border=0 height='226'> ");
out.println("<FORM name='frmlogin' action='loginservlet' onsubmit='return whensubmit()' method='post'>");
out.println("<p>账号<input maxLength='10' size='8' name='yhm'> 密码<input type='password' maxLength='6' size='8' value name='mm'><input type='submit' onclick='javascript:select()' value='登录'><INPUT type=hidden name=t></TD>");
out.println("<script languange='JavaScript' type='text/javascript'>");
out.println("<!--");
out.println("function whensubmit(){");
out.println("if(document.frmlogin.yhm.value==''){");
out.println("window.alert('请输入用户帐号');");
out.println("document.frmlogin.yhm.focus()");
out.println("return false;");
out.println("}");
out.println("if(document.frmlogin.mm.value==''){");
out.println("window.alert('请输入用户密码');");
out.println("document.frmlogin.mm.focus()");
out.println("return false;");
out.println("}");
out.println("}");
out.println("//-->");
out.println("</SCRIPT>");
out.println("</form>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession(true);
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String username = "system";
String password = "manager";
String conName = "jdbc:odbc:test";
String yhm = request.getParameter("yhm").trim();
yhm=toChinese(yhm);
String mm = request.getParameter("mm").trim();
Connection sqlCon;
ResultSet rstSql;
Statement stmS;
String strCon;
String strSql;
ResultSetMetaData rsmd;
String sql = "select * from wl.yhxx where yhm = '"+yhm+"' and mm='"+mm+"' and yhzt='1'";
if(!yhm.equals("")){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
sqlCon = java.sql.DriverManager.getConnection(conName, username,
password);
stmS = sqlCon.createStatement();
rstSql = stmS.executeQuery(sql);
rsmd = rstSql.getMetaData();
out.println("</body></html>");
long rowcount = 0;
while (rstSql.next()) {
rowcount++;
}
if (rowcount > 0) {
session.setAttribute("login", yhm);
out.println("<SCRIPT LANGUAGE=JavaScript TYPE='text/javascript'>");
out.println("<!--");
out.println("{");
out.println("window.opener = null;");
out.println("window.close('loginservlet')");
out.println("window.open('mainservlet','','fullscreen=yes')");
out.println("}");
out.println("-->");
out.println("</SCRIPT>");
}
else {
out.println("<SCRIPT LANGUAGE=JavaScript TYPE='text/javascript'>");
out.println("<!--");
out.println("{");
out.println("window.alert('口令不对,请再次输入')");
out.println("}");
out.println("-->");
out.println("</SCRIPT>");
}
if (sqlCon != null)
sqlCon.close();
if (rstSql != null)
rstSql.close();
if (stmS != null)
stmS.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
doGet(request, response);
}
}
...全文
31 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
dby_ 2003-10-31
  • 打赏
  • 举报
回复
这个程序看起来是有点累,改成jsp会好点,而且jsp和你其它的servlet连接并没有什么问题。会很简单
junyi2003 2003-10-31
  • 打赏
  • 举报
回复
楼上说得好。
javacofe 2003-10-31
  • 打赏
  • 举报
回复
推荐使用Struts实现哦
凋零的老树 2003-10-31
  • 打赏
  • 举报
回复
不用MVC,干脆都用JSP,Servlet不适合用的地方你勉强用了看起来还是麻烦,我看了一半实在看不动,太累,都用JSP吧,也简单
hj12 2003-10-31
  • 打赏
  • 举报
回复
你再犯罪!我实在看不下去了!去看看MVC!
ccccffff 2003-10-31
  • 打赏
  • 举报
回复
up下,给点份哦
hchcsdn 2003-10-31
  • 打赏
  • 举报
回复
关键是我都是用Java Servlet写的,如果直接是HTMl那很简单
nenya 2003-10-29
  • 打赏
  • 举报
回复
看了你的程序,我只想说.重写.有空可以看看关于MVC的一些介绍.
yuzs2000 2003-10-29
  • 打赏
  • 举报
回复
天!!!!
应该用JSP来表现前台,SERVLET来做控制
ajxn 2003-10-29
  • 打赏
  • 举报
回复
不过你这样编程是不太规范的!!!!标单页面应用HTML文件写,处理提交则用JSP(好一点)/servlet!
ajxn 2003-10-29
  • 打赏
  • 举报
回复
你可以在表单中写一隐藏文本框表示是第一次登录还是第二次提交;
然后在DOGET方法中判断是第一次提交还是第二次提交,第一次提交还是用DOGET方法中的代码,第二次提交则用DOPOST方法中的代码。
yugona 2003-10-29
  • 打赏
  • 举报
回复
你这程序实在很糟糕的说,前后台不分!
登陆界面(login.jsp)----servlet(loginOk)判断是否合法用户----合法--->
----不合法---->login.jsp
jiejifeng 2003-10-29
  • 打赏
  • 举报
回复
我的也是,上午就发了贴,现在还没个回复的
onetime 2003-10-29
  • 打赏
  • 举报
回复
同情中。我的问题也没人答!不知道高高手们哪里去了

81,115

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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