package com.been;
import java.sql.*;
public class LoginDate {
Connection con=null;
public void setupConn(){

try{
if(con==null);
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=date;user=sa;password=sa");
}
}catch(Exception e){

e.printStackTrace();
}



}
public int CheckLogin(String userName,String userPassword,int lang){
int result=-1;
try{
setupConn();
String sql="select * from admin where user_name=? and user_pwd=? and user_lang=?";
ResultSet rs;
PreparedStatement query=con.prepareStatement(sql);
query .setString(1,userName);
query.setString(2,userPassword );
query.setInt(3,lang);
rs=query.executeQuery();
if(rs.next()){
String nameData=rs.getString(2);
String passwordDate=rs.getString(3);
if((userName.compareTo(nameData)==0)&&(userPassword.compareTo(passwordDate)==0)){
//意思是如果userName这个字符串变量的值和nameData这个字符串变量的值相等,
//并且userPassword这个字符串变量的值和passwordDate这个字符串变量的值也相等的话,条件成立
if(lang==0){
result=0;

}else{
result=1;
}
}else{

result=-1;
}
}else{
result=-1;

}
}catch(Exception e){
e.printStackTrace();
return -1;

}

return result;
}}
package com.Mservlet;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import com.been.*;
public class Login extends HttpServlet {
private static final String CONTENT_TYPE="text/html;charset=GBK";
public void init()throws ServletException{

} public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("CONTENT_TYPE");
request.setCharacterEncoding("GBK");
ServletContext application=getServletContext();
HttpSession session=request.getSession();
RequestDispatcher requestDispatcher=null;
String userName=request.getParameter("userName");
String userPassword=request.getParameter("userPassword");
String lang=request.getParameter("lang");
LoginDate loginDate=new LoginDate();
if(userName.equals(null)||userName.equals("")){
requestDispatcher=application.getRequestDispatcher("/error.jsp");
request.setAttribute("infor", "用户名字和用户密码不允许空."+"请<a href=\"javascript:history.back()\">"+"请重新输入</a>.");
requestDispatcher.forward(request, response);
return;
}
int result=loginDate.CheckLogin(userName, userPassword, Integer.parseInt(lang));
switch(result){
case 0:
session.setAttribute("infor", "该用户是管理员");
break;
case 1:
session.setAttribute("infor", "改用户是用户");
break;
case -1:
session.setAttribute("infor", "用户或者密码输入错误");
break; }
}
public void destroy(){
}
}

<%@ page contentType="text/html; charset=GBK"%>
<jsp:useBean id="login" scope="session" class="com.been.LoginDate"></jsp:useBean>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<% request.setCharacterEncoding("GBK");
String  infor="";
if(session.getAttribute("infor")!=null){
infor=(String)session.getAttribute("infor");} %>
<!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>  
  <form method="post" action="/new/Login"><p><input type="text" name="userName"></p><p><input type="password" name="userPassword"></p><select size="1" name="lang">
<option value="1">用户</option>
<option value="0">管理员</option>
</select>
<p>&nbsp;<input type="Submit"  value="登陆">  <input type="reset" name="button3"></p></form>
<h3><%=infor %></h3>
  </body>
</html>为什么按提交后..会弹出一个下载页面

解决方案 »

  1.   

    点提交后url栏中显示的是什么
    是不是你想要的路径啊
    <form method="post" action="/new/Login">你上面写的是不是不对呢
      

  2.   

    我按提交后..
    url没变..但就是弹出一个框咯
      

  3.   

    检查一下你的web.xml这个配置文件
      

  4.   

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <servlet>
        <description>This is the description of my J2EE component</description>
        <display-name>This is the display name of my J2EE component</display-name>
        <servlet-name>Login</servlet-name>
        <servlet-class>com.Mservlet.Login</servlet-class>
      </servlet>
      <servlet>
        <description>This is the description of my J2EE component</description>
        <display-name>This is the display name of my J2EE component</display-name>
        <servlet-name>Lnew</servlet-name>
        <servlet-class>com.Mservlet.Lnew</servlet-class>
      </servlet> 
     <servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/Login.do</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>Lnew</servlet-name>
        <url-pattern>/SelectBeer.do</url-pattern>
      </servlet-mapping><welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>这个是配置~~ 应该没问题吧