我有一个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>密 码:</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
在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>密 码:</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
解决方案 »
- java设置Date的json输出格式的问题
- 两个系统之间信息通知
- 浏览器怎么连不上tomcat?
- 重分相送,并且随便带了个很有意思的问题,进来看看吧
- 很菜的 jsp传值问题 分少 有愿意帮忙的吗 在线等
- Servlet运行是出现下面问题
- 为什么 有的时候 从session里面取出对象 并对其赋值后 session里面对象的值会改变而有的时候不行?就是从session里面getAttribute出来的
- 如何设置TomCat连接 MS SQL Server 7 的连接池阿?
- 怎样速成jsp,给分100
- SOS!!怎样根据数据库读出的数据以checkbox的形式显示查询结果??分不是问题我会全部奉上!
- 在jsp页面勾选复选框时执行Action里面的方法。
- 关于infomix下setQueryTimeOut的问题
<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一下
appName是你的应用路径。或者你把LoginConf加个包名。
转就好像没执行
说明配置没大多问题,确认提交form后,提交的url是什么,然后相应的修改form中的action属性。
submit之后是http://localhost:1228/StructLearning/LoginConf显示一片空白
<form action="/appName/LoginConf">因为在web.xml中配置Servlet时,默认是当前应用下的,即所有的Servlet目录是应用目录加上你配置的Servlet路径。
如果不写appName则表示网站的根目录,所以不能用。
在LoginConf类中方法protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
是空的,没有任何code,所以是空的,form表单提交方式你设置成post了,但是url是get方式
将doget方法copy到post方法中即可
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}