servlet配置
我有一个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>
<%@ 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>密 码:</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>
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 {
}
}
import 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;
}
}
}
}
<url-pattern>/LoginConf</url-pattern>
</servlet-mapping>
<servlet-class>LoginConf</servlet-class>这个类执行了吗,DEBUG一下
[解决办法]
<form action="/LoginConf" method="post">
直接在web.xml中配置的Servlet访问时需要和配置的路径相同。Struts是转发过去的,不需要单独在web.xml中配置
[解决办法]
访问地址:http://ipaddress:port/appName/LoginConf
appName是你的应用路径。
或者你把LoginConf加个包名。
[解决办法]
login.jsp登陆按钮提交无结果,但是在地址栏直接输入却是登陆成功。
说明配置没大多问题,确认提交form后,提交的url是什么,然后相应的修改form中的action属性。
[解决办法]
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}