gpt4 book ai didi

java - 空白页面作为 servlet 中的输出出现

转载 作者:行者123 更新时间:2023-11-29 17:07:36 25 4
gpt4 key购买 nike

我正在尝试在 SQL 数据库中插入员工详细信息,但是当我运行该程序时,它在输入员工详细信息输出后显示注册页面,并且显示为空白页面

这是我的 web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>CreateServlet</servlet-name>
<servlet-class>com.sai.CreateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CreateServlet</servlet-name>
<url-pattern>/CreateServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

这是我的 servlet 类

public class CreateServlet extends HttpServlet 
{


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection con=null;
Statement st=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection("jdbc:mysql://localhost:3306/dbemployee","root","");
con.createStatement();
String id=request.getParameter("UserName");
String pwd=request.getParameter("Password");
String eAdd=request.getParameter("EmpAddress");
String gender=request.getParameter("MaleorFemale");
String email=request.getParameter("Email");
String lang=request.getParameter("languages");
String nation=request.getParameter("Nationality");
String date=request.getParameter("RegDate");

String sql="insert into Employee Values ("+id+" "+pwd+" "+eAdd+" "+gender+" "+email+" "+lang+" "+nation+" "+date+")";
st.executeUpdate(sql);
if(id !=null | pwd != null | eAdd!=null | gender !=null |email !=null | lang !=null | nation!=null | date !=null)
{
RequestDispatcher rd = request.getRequestDispatcher("/success.html");
rd.forward(request, response);
}
else
{
out.println("<font color=red>Please fill all the fields</font>");
RequestDispatcher rd = request.getRequestDispatcher("/index.html");
rd.forward(request, response);

}
}catch(ClassNotFoundException | SQLException | NullPointerException e)
{
e.printStackTrace();
}
finally
{
try
{
if(st!=null) st.close();
if(con!=null)con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

这是我的index.html,我在其中创建了注册文档

<body>
<form name="EmployeeReg" method="post" action="CreateServlet">
UserName:&nbsp <input type="text" name="UserName" placeholder="username"><br>
Password:&nbsp &nbsp&nbsp<input type="password" name="password"><br>
Address :&nbsp&nbsp&nbsp&nbsp <input type="text" name="EmpAdress" placeholder="EmpAddress" ><br>
Gender : &nbsp&nbsp&nbsp<input type="radio" name="MaleorFemale" value="Male">MALE<br>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<input type="radio" name="MaleorFemale" value="Female">FEMALE<br>

EMail: &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <input type="text" name="email" placeholder="email"><br>
Languages Known: <input type="checkbox" name="languages" value="Telugu">Telugu
<input type="checkbox" name="languages" value="Kannada">Kannada
<input type="checkbox" name="languages" value="Hindi">Hindi
<input type="checkbox" name="languages" value="English">English
<input type="checkbox" name="languages" value="Tamil">Tamil
<input type="checkbox" name="languages" value="Malayalam">Malayalam<br>
Nationality: <select name="nationality">
<option value="">-- select one --</option>
<option value="afghan">Afghan</option>
<option value="albanian">Albanian</option>
<option value="algerian">Algerian</option>
<option value="american">American</option>
<option value="andorran">Andorran</option>
<option value="angolan">Angolan</option>
<option value="antiguans">Antiguans</option>
<option value="argentinean">Argentinean</option>
<option value="armenian">Armenian</option>
<option value="australian">Australian</option>
<option value="austrian">Austrian</option>
<option value="austrian">Indian</option>
</select><br>
Reg.Date: &nbsp&nbsp&nbsp<input type="date" id="RegDate" value="2010-07-77">
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myDate").value;
document.getElementById("demo").innerHTML = x;
}
</script>
<input type="submit" value="Submit">
</form>
</body>

最佳答案

要按照您显示的方式调用 servlet,必须实现 doPost 方法。它看起来像:

 public class CreateServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// and the rest of your method.
}
}

基本上,您必须将当前方法重命名为 doPost,以便在向其 POST 表单数据时调用它。

在您的浏览器中,您当前应该会收到某种错误。这不太可能记录到您的服务器日志中,但如果您使用浏览器的开发工具,那么您应该能够看到从服务器返回的可能是 400 或 405 错误。

就 @MarkRotteveel 提出的观点而言 - 不要将此代码发布到公共(public)站点,因为他 100% 正确 - 你很快就会遭受 SQL 注入(inject)攻击。

关于java - 空白页面作为 servlet 中的输出出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52005908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com