这是我的jsp代码: -
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div id="header">
<center>
<div id="over">
<p> <font size="18" color="Brown"><b>UBONA Technologies</b></font></p>
</div>
</center>
</div>
<br><br><br><br><br><br>
<form method="get" action="controller.java">
<div class="container">
<center>
<table tableborder=0>
<tr><td><label><font size="5" color="BLACK"><b>USERNAME</b></font></label></td>
<br>
<td><input type="text" placeholder="Enter User Name" name="username" class="inputi" required></td>
</tr><br>
<tr><td><label><font size="5" color="BLACK"><b>PASSWORD</b></font></label></td>
<br>
<td><input type="password" placeholder="Enter Password" name="password" class="inputi" required></td>
</tr><br>
</table><br>
<input type="submit" class="button" name="submit" value="LOGIN" />
</center></div>
</form>
</body>
</html>
这是我的Servlets代码: -
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.sql.*;
//@WebServlet("/controller")
public class controller extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e)
{
}
try
{
String uname = request.getParameter("username");
String paswd = request.getParameter("password");
Connection con= null;
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","6q190No6@");
Statement s = con.createStatement();
ResultSet rs=s.executeQuery("select * from credentials");
rs.next();
String username1 = rs.getString("username");
String password2 = rs.getString("password");
if(uname.equals(username1) && paswd.equals(password2))
{
response.sendRedirect("welcome.jsp");
}
else
{
response.sendRedirect("wrongpas.jsp");
}
rs.close();
}
catch(SQLException sqe)
{
System.out.println("home"+sqe);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
}
这是我的web.xml: -
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/controller</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:0)
如果您没有WEB-INF/classes
手动创建它并将课程保存在那里。
将您需要的罐子放入WEB-INF/lib/
。
首先将表单标记修改为
<form id="login" action="${pageContext.request.contextPath}/controller" method="GET">
添加ID属性
<input type="text" placeholder="Enter User Name" name="username" id="username" class="inputi" required>
也在这里
<input type="password" placeholder="Enter Password" name="password" id="username" class="inputi" required>
其次删除对注释的评论
//@WebServlet("/controller")
成功
@WebServlet(name = "controller", urlPatterns = {"/controller"})
导入注释
import javax.servlet.annotation.WebServlet;
删除doPost
它在您的情况下无效。
您也不需要web.xml
。
<强>建议强>
您的班级名称应以大写字母开头。
urlPatterns应与您的班级名称不同。
答案 1 :(得分:0)
以下更改将解决您的问题:
在JSP代码中:
<form role="form" method="get" action="${pageContext.request.contextPath}/controller">
在Servlet代码中:
@WebServlet("/controller")
添加上面的代码行将提示您导入Webservlet注释库:
import javax.servlet.annotation.WebServlet;
使用Uppercase开始Java类名称也是一种很好的做法 即 c ontroller.java 应为 C ontroller.java < / em>的