awaria z połączeniem html+servlet+mysql

0

Witam, zaczynam nauke i mam maly problem. Chcialem sobie stworzyc panel logowania. Wyglada to tak ze zrobilem sobie w html input na login i pass oraz button urachamiajacy servlet.
Kod w html:

 <form action="baza1.do">
           <input type="text" name="login"/>
           <input type="text" name="pass"/>
        <input type="submit" value="ok"/>

            </form> 

kod servleta:

package package1;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class baza1 extends HttpServlet {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/hue";
    static final String login = "root";
    static final String pass = "";
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        Connection conn = null;
        Statement stmt = null;
        String Login = req.getParameter("login");
        String Pass = req.getParameter("pass");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, login, pass);
            stmt = conn.createStatement();
            String sql = "SELECT login, pass FROM users WHERE login='"+Login+"' and pass='"+Pass+"'";
            ResultSet rs = stmt.executeQuery(sql);

            try (PrintWriter pw = res.getWriter()) 
            {
                res.setContentType("text/html");

                if (Login!=null && Pass!=null)
                {

                System.out.println("udalo sie");
                }
                else
                {
                    pw.println("Login Failed...!");
                }
            }
            conn.close();
        } catch (ClassNotFoundException | SQLException e) {
        }

    }

Problem jest taki że gdy sobie odpale projekt, podam login i haslo i odpale servlet to pojawia mi się komunikat:

HTTP Status 405 - HTTP method GET is not supported by this URL

type Status report

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource.

Apache Tomcat/8.0.27 

Oczywiście mam stworzona bazę danych z użytkownikami.
Może problem jest banalny do rozwiązania ale tak jak napisalem dopiero zaczynam, a w sieci na nic nie trafiłem co mogło by mi pomóc.
Z góry dziękuje za pomoc.

0

A gdzie przed serwletem adnotacja:

@WebServlet("baza1.do")
public class baza1 extends HttpServlet

Chyba że konfigurujesz serwlety bez adnotacji?

0

nie daje adnotacji.

1

Nie widzę w klasie baza1 metody doGet.

0

dodalem metode doget:

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

teraz pojawia mi sie taki oto komunikat:

 HTTP Status 500 - Not supported yet.

type Exception report

message Not supported yet.

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.UnsupportedOperationException: Not supported yet.
    package1.baza1.processRequest(baza1.java:68)
    package1.baza1.doGet(baza1.java:64)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.27 logs.

Apache Tomcat/8.0.27
0

Zdradzisz jak wygląda tenpackage1.baza1.processRequest(baza1.java:68)wiersz?

0
@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
0

Gdzie jest wiersz - 68 w pliku baza1.java - z metody processRequest?

0

nie ma nic


package package1;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class baza1 extends HttpServlet {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/hue";
    static final String login = "root";
    static final String pass = "";
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        Connection conn = null;
        Statement stmt = null;
        String Login = req.getParameter("login");
        String Pass = req.getParameter("pass");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, login, pass);
            stmt = conn.createStatement();
            String sql = "SELECT login, pass FROM users WHERE login='"+Login+"' and pass='"+Pass+"'";
            ResultSet rs = stmt.executeQuery(sql);

            try (PrintWriter pw = res.getWriter()) 
            {
                res.setContentType("text/html");

                if (Login!=null && Pass!=null)
                {

                System.out.println("udalo sie");
                }
                else
                {
                    pw.println("Login Failed...!");
                }
            }
            conn.close();
        } catch (ClassNotFoundException | SQLException e) {
        }

    }

 @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

    private void processRequest(HttpServletRequest request, HttpServletResponse response) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
0

Nic nie ma? A co to

 throw new UnsupportedOperationException("Not supported yet.");

jest? Zażądałeś rzucenia wyjątku i dziwisz się, że wyjątek się pojawił?
Ty cokolwiek rozumiesz z tego co piszesz. Ten kod, który jest w metodzie doPost powinien się znaleźć w metodzie doGet.

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0