我正在尝试一些基本的Servlet编程。我创建了一个基本的Html表单,在提交它时,想要调用一个servlet来打印用户输入的名称。我发布下面的代码。
HTML: (它位于rootfolder中 - webapps \ helloworld \ hello.html)
<html>
<body>
<form action="./hello" method="get">
Name: <input type="text" name="P1">
<input type="submit" value="Submit">
</form>
</body>
</html>
&GT;的web.xml (位置:webapps \ helloworld \ WEB_INF \ web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>hs</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hs</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
&GT; Servlet类 (位置:webapps \ helloworld \ WEB_INF \ classes \ HelloWorld.class
import javax.servlet.*;
import java.io.*;
public class HelloWorld implements Servlet{
public void init(ServletConfig sc)throws ServletException{
//initialization code
}
public ServletConfig getServletConfig(){
return null;
}
public void service(ServletRequest request, ServletResponse response)throws ServletException,IOException{
String name=request.getParameter("P1");
PrintWriter out = response.getWriter();
out.println("Hello: "+name);
}
public String getServletInfo(){
return null;
}
public void destroy(){
}
}
因此,每当我输入名称并点击提交时,我都会收到HTTP状态404&#39;错误。你能告诉我我做错了什么!任何帮助将非常感激。谢谢!
答案 0 :(得分:0)
在HTML表单中,您必须将表单操作专门提供给您的servlet“private SampleDBEntities1 db = new SampleDBEntities1();
public ActionResult Index(string id)
{
var userEmail = User.Identity.Name;
var model = db.Staffs.Where(i => i.Email == userEmail);
var model1 = model.CurrentApplications;
var ummm = new LeaveIndexData { Staffs = model, CurrentApplications = model1 };
return View(ummm);
}
”。因为HelloWorld
是显示数据路径的人
form action
HTTP 404 未找到错误表示无法在服务器上找到您尝试访问的网页
或尝试这个servlet代码:
<html>
<body>
<form action="/HelloWorld" method="get">
Name: <input type="text" name="P1">
<input type="submit" value="Submit">
</form>
</body>
</html>