当我正在运行create-dest.jsp并填写表单时,我正在接受此错误,任何人都可以提供帮助,并建议我一个解决方案?
PS:我知道在jsp中编写java是不好的......
输入例外报告
message处理JSP页面时发生异常 第35行的/assets/jsp/create-dest-code.jsp
description服务器遇到阻止它的内部错误 完成此请求。
异常org.apache.jasper.JasperException:发生异常 在第35行处理JSP页面/assets/jsp/create-dest-code.jsp
32://out.print(id); 33:if(cat!= null){34:for(int I = 0;我 } else {
堆栈跟踪: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:461) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
根本原因javax.servlet.ServletException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:未知列 '冬季'在' where子句' org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838) org.apache.jsp.assets.jsp.create_002ddest_002dcode_jsp._jspService(create_002ddest_002dcode_jsp.java:173) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 未知专栏'冬季'在' where子句' com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870) com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573) com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665) com.mysql.jdbc.Connection.execSQL(Connection.java:3118) com.mysql.jdbc.Connection.execSQL(Connection.java:3047) com.mysql.jdbc.Statement.executeQuery(Statement.java:1166) org.apache.jsp.assets.jsp.create_002ddest_002dcode_jsp._jspService(create_002ddest_002dcode_jsp.java:140) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
注意Apache中提供了根本原因的完整堆栈跟踪 Tomcat / 8。0。3。3日志。 创建-dest.jsp
<form name="createdest" method="get" action="../jsp/create-dest-code.jsp">
Country: <input type="text" required name="id8" /> <br>
City: <input type="text" required name="id9" /> <br>
URL Video: <input type="url" required name="id10" /> <br> <br>
<i><ins>Categorize the destination (max 2): </ins></i> <br> <br>
<input type="checkbox" name="dest1" value="Winter" >Winter<br>
<input type="checkbox" name="dest1" value="Christmas" >Christmas <br>
<input type="checkbox" name="dest1" value="Summer" >Summer <br> <br>
<input type="submit" class="button" value="CREATE DESTINATION" > />
<input type="hidden" name="varname" value="" />
</form>
创建-DEST-code.jsp
<%
String id8=request.getParameter("id8"); //Country field
String id9=request.getParameter("id9"); //City field
String id10=request.getParameter("id10"); //URL field
String cat[]=request.getParameterValues("dest1"); //categories
int status, id , id1=0 ;
Class.forName("com.mysql.jdbc.Driver");
String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234";
Connection myConnection = DriverManager.getConnection(myDatabase);
Statement myStatement = myConnection.createStatement();
Statement myStatement2 = myConnection.createStatement();
String sqlInsert = "INSERT INTO dest(Country,City,URL) VALUES ('"+id8+"', '"+id9+"','"+id10+"')";
status = myStatement.executeUpdate(sqlInsert);
if (status ==0 ) {
throw new SQLException("INSERT INTO DEST FAILED!");
}
ResultSet rs = myStatement.executeQuery("SELECT idDest FROM dest WHERE Country='"+id8+"' AND City='"+id9+"' AND URL='"+id10+"'" );
if (rs.next()) {
id=rs.getInt(1);
if ( cat != null) {
for(int i=0;i<cat.length;i++) {
rs = myStatement.executeQuery("select idCategories from categories where Category_name="+cat[i]+" ");
if (rs.next()) {
id1 = rs.getInt(1);
} else {
throw new SQLException("ERROR");
}
status = myStatement2.executeUpdate("INSERT INTO Dest_has_Categories (Dest_idDest,Categories_idCategories) VALUES ('"+id+"','"+id1+"')");
if ( status ==0 ) {
throw new SQLException("INSERT INTO DEST_HAS_CATEGORIES FAILED!");
}
}
}
} else {
throw new SQLException("INSERT INTO DEST FAILED! NO ID OBTAINED!");
}
myStatement.close();
myStatement2.close();
myConnection.close();
%>
mysql表:
CREATE TABLE IF NOT EXISTS `project_app`.`Dest` (
`idDest` INT NOT NULL AUTO_INCREMENT,
`Country` VARCHAR(45) NOT NULL,
`City` VARCHAR(45) NOT NULL,
`URL` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idDest`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Categories`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `project_app`.`Categories` (
`idCategories` INT NOT NULL,
`Category_name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idCategories`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Dest_has_Categories`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `project_app`.`Dest_has_Categories` (
`Dest_idDest` INT NOT NULL AUTO_INCREMENT,
`Categories_idCategories` INT NOT NULL,
PRIMARY KEY (`Dest_idDest`, `Categories_idCategories`),
INDEX `fk_Dest_has_Categories_Categories1_idx` (`Categories_idCategories` ASC),
INDEX `fk_Dest_has_Categories_Dest_idx` (`Dest_idDest` ASC),
CONSTRAINT `fk_Dest_has_Categories_Dest`
FOREIGN KEY (`Dest_idDest`)
REFERENCES `mydb`.`Dest` (`idDest`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Dest_has_Categories_Categories1`
FOREIGN KEY (`Categories_idCategories`)
REFERENCES `mydb`.`Categories` (`idCategories`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
INSERT INTO categories(idCategories,Category_name) VALUES (1,'Winter');
INSERT INTO categories(idCategories,Category_name) VALUES (2,'Christmas');
INSERT INTO categories(idCategories,Category_name) VALUES (3,'Summer');
答案 0 :(得分:3)
针对胜利的SQL注入攻击漏洞:
rs = myStatement.executeQuery("[..snip..] Category_name="+cat[i]+"");
cat[i]
在查询中没有引用,因此您将外部输出直接传递到查询中,从而生成以下语句:
SELECT ... Category_name=Winter
生成确切的错误消息。
忽略正确的SQL编写实践,您需要以下内容:
rs = myStatement.executeQuery("[..snip..]Category_name='"+cat[i]+"' ");
^----------^
请注意额外的'
。