doPost(HttpServletRequest,HttpServletResponse)错误

时间:2019-01-11 20:37:53

标签: java html servlets

我在eclipse中的Java servlet无法正常工作,但是我不知道为什么,在所有servlet上,当我单击时,我都会遇到相同的错误 doPost(HttpServletRequest,HttpServletResponse)标记,在那里他们写信给我。类型为Class的forName(java.lang.String)方法不适用于参数(String)。请任何人帮助我,这个错误很烦人。 这是我的代码:(对不起,因为我的代码不是全部用英语)

package www.autobuskastanica.com;

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;

@WebServlet({"/HeaderServlet","/header.jsp"})
public class HeaderServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;

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

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

        String imeSesije = (String) request.getSession().getAttribute("email");
        String out = "";
        response.setCharacterEncoding("UTF-8");

        if(imeSesije == null) {
             out += "   <head>  "  + 
             "   <meta charset=\"UTF-8\">  "  + 
             "   </head>  "  + 
             "     "  + 
             "     "  + 
             "   <header>  "  + 
             "         <div class=\"navbar navbar-fixed-top\">  "  + 
             "           <div class=\"navbar-inner\">  "  + 
             "             <div class=\"container\">  "  + 
             "               <a href=\"home.jsp\" class=\"brand brand-bootbus\">Autobuska stanica</a>  "  + 
             "               <button type=\"button\" class=\"btn btn-navbar\" data-toggle=\"collapse\" data-target=\".nav-collapse\">  "  + 
             "                 <span class=\"icon-bar\"></span>  "  + 
             "                 <span class=\"icon-bar\"></span>  "  + 
             "                 <span class=\"icon-bar\"></span>  "  + 
             "               </button>  "  + 
             "               <div class=\"nav-collapse collapse\">          "  + 
             "                 <ul class=\"nav pull-right\">  "  + 
             "                   <li><a href=\"red_voznje.jsp\">Red vožnje</a></li>  "  + 
             "                   <li><a href=\"kupi_kartu.jsp\">Kupi kartu</a></li>  "  + 
             "                   <li><a href=\"kontakt.jsp\">Kontakt</a></li>  "  + 
             "                   <li><a href=\"registracija.jsp\">Registruj se</a></li>  "  + 
             "                   <li><a href=\"login.jsp\">Prijavi se</a></li>  "  + 
             "                 </ul>  "  + 
             "               </div>  "  + 
             "             </div>  "  + 
             "           </div>  "  + 
             "         </div>  "  + 
             "      </header>  " ; 
        }else {
            String url = "jdbc:sqlserver://localhost:1433;databaseName=AutobuskaStanica;user=sa;password=root";
            Connection con = null;      

                try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    con = DriverManager.getConnection(url);
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery("SELECT * FROM Korisnici WHERE email='" + imeSesije+"'");
                    while(rs.next()) {
                        if(rs.getString("uloga").equals("korisnik")){
                        out += "   <head>  "  + 
                                 "   <meta charset=\"UTF-8\">  "  + 
                                 "   </head>  "  + 
                                 "     "  + 
                                 "   <header>  "  + 
                                 "         <div class=\"navbar navbar-fixed-top\">  "  + 
                                 "           <div class=\"navbar-inner\">  "  + 
                                 "             <div class=\"container\">  "  + 
                                 "               <a href=\"home.jsp\" class=\"brand brand-bootbus\">Autobuska stanica</a>  "  + 
                                 "               <button type=\"button\" class=\"btn btn-navbar\" data-toggle=\"collapse\" data-target=\".nav-collapse\">  "  + 
                                 "                 <span class=\"icon-bar\"></span>  "  + 
                                 "                 <span class=\"icon-bar\"></span>  "  + 
                                 "                 <span class=\"icon-bar\"></span>  "  + 
                                 "               </button>  "  + 
                                 "               <div class=\"nav-collapse collapse\">          "  + 
                                 "                 <ul class=\"nav pull-right\">  "  + 
                                 "                   <li><a href=\"red_voznje.jsp\">Red vožnje</a></li>  "  + 
                                 "                   <li><a href=\"kupi_kartu.jsp\">Kupi kartu</a></li>  "  + 
                                 "                   <li><a href=\"kontakt.jsp\">Kontakt</a></li>  "  + 
                                 "                   <li class=\"dropdown\">  "  + 
                                 "                     <a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\">"+rs.getString("ime")+"<b class=\"caret\"></b></a>  "  + 
                                 "                     <ul class=\"dropdown-menu\">  "  + 
                                 "                       <li><a href=\"logout.jsp\">Odjavite se</a></li>  "  + 
                                 "                     </ul>  "  + 
                                 "                  </li>  " +
                                 "                 </ul>  "  + 
                                 "               </div>  "  + 
                                 "             </div>  "  + 
                                 "           </div>  "  + 
                                 "         </div>  "  + 
                                 "  </header>  " ;
                        }
                        else if(rs.getString("uloga").equals("admin")) {
                            out += "   <head>  "  + 
                                     "   <meta charset=\"UTF-8\">  "  + 
                                     "   </head>  "  + 
                                     "     "  + 
                                     "   <header>  "  + 
                                     "         <div class=\"navbar navbar-fixed-top\">  "  + 
                                     "           <div class=\"navbar-inner\">  "  + 
                                     "             <div class=\"container\">  "  + 
                                     "               <a href=\"home.jsp\" class=\"brand brand-bootbus\">Autobuska stanica</a>  "  + 
                                     "               <button type=\"button\" class=\"btn btn-navbar\" data-toggle=\"collapse\" data-target=\".nav-collapse\">  "  + 
                                     "                 <span class=\"icon-bar\"></span>  "  + 
                                     "                 <span class=\"icon-bar\"></span>  "  + 
                                     "                 <span class=\"icon-bar\"></span>  "  + 
                                     "               </button>  "  + 
                                     "               <div class=\"nav-collapse collapse\">          "  + 
                                     "                 <ul class=\"nav pull-right\">  "  + 
                                     "                   <li><a href=\"red_voznje.jsp\">Red vožnje</a></li>  "  + 
                                     "                   <li><a href=\"kupi_kartu.jsp\">Pregled karata</a></li>  "  + 
                                     "                   <li><a href=\"kontakt.jsp\">Kontakt</a></li>  "  + 
                                     "                   <li class=\"dropdown\">  "  + 
                                     "                     <a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\">"+rs.getString("ime")+"<b class=\"caret\"></b></a>  "  + 
                                     "                     <ul class=\"dropdown-menu\">  "  + 
                                     "                       <li><a href=\"logout.jsp\">Odjavite se</a></li>  "  + 
                                     "                     </ul>  "  + 
                                     "                  </li>  " +
                                     "                 </ul>  "  + 
                                     "               </div>  "  + 
                                     "             </div>  "  + 
                                     "           </div>  "  + 
                                     "         </div>  "  + 
                                     "  </header>  " ;
                        }
                    }
                }catch(Exception e) {
                    e.printStackTrace();
                }finally {
                    if(con != null) {
                        try {                       
                            con.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        PrintWriter pw = response.getWriter();
        pw.println(out);
        }
    }

0 个答案:

没有答案