我有一个LoginConf类
在login.jsp里面要访问它:
<form action="LoginConf" method="post">
如何配置web.xml
以下是我的配置:(LoginConf没有设置包名):
       <servlet>
     <servlet-name>LoginConf</servlet-name>
     <servlet-class>LoginConf</servlet-class>
    </servlet>
    <servlet-mapping>
     <servlet-name>LoginConf</servlet-name>
     <url-pattern>/LoginConf</url-pattern>
    </servlet-mapping>但是当我通过login.jsp访问serlet时页面没有返回结果
login.jsp::<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>用户登录</title>
</head>
<body>
<h2>用户登录</h2>
<form action="LoginConf" method="post">
<table>
    <tr>
        <td>用户名:</td>
        <td><input type="text" name="uname" /></td>
    </tr>
    <tr>
        <td>密&nbsp;&nbsp;码:</td>
        <td><input type="password" name="password" />
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="登录"/>
            <input type="reset" value="重置"/>
        </td>
    </tr>
</table>
</form>
</body>
</html>而我修改浏览器地址为http://localhost:1228/StructLearning/LoginConf?uname=admin&password=admin时,显示登录成功
以下是LoginConf.java:
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/**
 * Servlet implementation class LoginConf
 */
@WebServlet(description = "登录检测", urlPatterns = { "/LoginConf" })
public class LoginConf extends HttpServlet {
private static final long serialVersionUID = 1L; /**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
// 接受用户参数
String name = request.getParameter("uname");
out.write("name:"+name);
String password = request.getParameter("password");
out.write("password:"+password);
LoginCheck lc = new LoginCheck();
if (lc.isLogin(name, password)) {
// 登录成功
request.setAttribute("login", true);
request.getRequestDispatcher("login_success.jsp").forward(request,
response);
out.write("<h1>Success</h1>");
} else {
// //登录失败
//// request.getRequestDispatcher("login.jsp")
// .forward(request, response);
out.write("<h1>FALSE</h1>");
}
} /**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException { }}
和LoginCheck.javaimport java.sql.*;/**
 * @author MrChen 登录检测
 */
public class LoginCheck {
/**
 * isLogin()
 * 
 * @param name
 *            String
 * @param password
 *            String 判断用户是否合法
 * */
public boolean isLogin(String name, String password) {
Connection conn=null;
Statement stmt=null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost/mybbs?user=root&password=chen911228";// 连接字符串
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
String sql = String.format(
"select * from users where username='%s'&&password='%s'",
name, password);
rs = stmt.executeQuery(sql);
return rs.next();
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException e) {
e.printStackTrace();
return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
finally{
try {

if(rs!=null){
rs.close();
rs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
}if(conn!=null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
}求帮忙为什么,好像form表单没有传参数似的或者说servlet好像根本没有执行啊?servletformactionweb.xml

解决方案 »

  1.   

        <servlet>
            <servlet-name>LoginConf</servlet-name>
            <servlet-class>LoginConf</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>LoginConf</servlet-name>
            <url-pattern>/LoginConf</url-pattern>
        </servlet-mapping>        <servlet-class>LoginConf</servlet-class>这个类执行了吗,DEBUG一下
      

  2.   

    就是在浏览器地址栏中在此敲得话就执行而通过login.jsp的form好像没有执行
      

  3.   

    额才开始学,都不知道怎么debug它
      

  4.   

    <form action="/LoginConf" method="post">直接在web.xml中配置的Servlet访问时需要和配置的路径相同。Struts是转发过去的,不需要单独在web.xml中配置
      

  5.   

    这样不行诶。HTTP Status 500 - Error instantiating servlet class LoginConf
      

  6.   

    访问地址:http://ipaddress:port/appName/LoginConf
    appName是你的应用路径。或者你把LoginConf加个包名。
      

  7.   

    是啊,直接访问可以。但从login.jsp
    转就好像没执行
      

  8.   

    login.jsp登陆按钮提交无结果,但是在地址栏直接输入却是登陆成功。
    说明配置没大多问题,确认提交form后,提交的url是什么,然后相应的修改form中的action属性。
      

  9.   

    <form action="LoginConf" method="post" >
    submit之后是http://localhost:1228/StructLearning/LoginConf显示一片空白
      

  10.   

    你的意思是加了appName就可以访问了是吧,在form中要这样写
    <form action="/appName/LoginConf">因为在web.xml中配置Servlet时,默认是当前应用下的,即所有的Servlet目录是应用目录加上你配置的Servlet路径。
    如果不写appName则表示网站的根目录,所以不能用。
      

  11.   


    在LoginConf类中方法protected void doPost(HttpServletRequest request, 
                HttpServletResponse response) throws ServletException, IOException { 
      
        } 
    是空的,没有任何code,所以是空的,form表单提交方式你设置成post了,但是url是get方式
    将doget方法copy到post方法中即可
      

  12.   


     protected void doPost(HttpServletRequest request,             
            HttpServletResponse response) throws ServletException, IOException {       
    doGet(request,response);