我只能使用Netbeans IDE处理企业软件项目,而且没有框架。
前端显示为register.jsp
。我的模型包包含Customer.java
类,包含一些getter和setter。数据包包含“CustomerData.java”,其中包含与客户相关的数据库功能:注册,登录等。CustomerData
扩展HttpServlet
。
我需要从我的注册表单中引用CustomerData
类中的特定方法。这可能吗?
如果可以这样做,web.xml
文件条目对于servlet
和servletmapping
应该是什么?
这是代码。
Register.jsp
<form name="loginForm" method="post" action="CustomerData/RegisterCustomer">
......
</form>
CustomerData.java 骨架:
public class CustomerData extends HttpServlet {
public void registerCustomer(HttpServletRequest request)
throws ServletException, IOException
{
// this is the method I need to reference. It creates a db connection, checks to see if
// the Customer is already in the DB, and if not, registers the user.
}
public void loginCustomer(HTTPServlet request)
throws ServletException, IOException
{
// Some other Customer data method that will need to be called from my login.jsp page
}
public void SomeOtherMethod()
{
// some helper methods or validation methods for Customer
}
}
答案 0 :(得分:0)
我会向你推荐以下内容。
在JSP页面中,您可以定义一个参数,例如opr
,您可以在其中设置操作的值。
<form name="loginForm" method="post" action="CustomerData/">
<input type=hidden name=opr id=opr value=1
......
</form>
在Servlet中,您可以通过以下传递的操作值来处理操作
public doPost(HttpServletRequest req, HttpServletResponse res){
int operation = Integer.valueOf(req.getParameter("opr"));
if (operation == 1){
registerCustomer(req);
}else if (operation == 2){
loginCustomer(req);
}else if (operation == 3){
SomeOtherMethod();
}...
}
希望这会对你有所帮助。
答案 1 :(得分:0)
您可以使用getPathInfo
完成您想要做的事情假设您的servlet映射是
<servlet-mapping>
<servlet-name>CustomerData</servlet-name>
<url-pattern>/CustomerData/*</url-pattern>
</servlet-mapping>
调用
String pathInfo = request.getPathInfo();
会在pathInfo中为您提供值'/RegisterCustomer'
。从那里开始,找出需要调用的方法应该是相当简单的。不要忘记广告检查代码来处理可能在servlet上抛出的各种滥用行为(例如,没有给出“方法名称”,指定了不存在的方法名称等)。