在url中传递参数以调用servlet [英] Passing parameter in a url to call servlet

查看:93
本文介绍了在url中传递参数以调用servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友您好,有人可以告诉我如何在url中传递参数来调用servlet.我没有创建任何.jsp文件,即没有UI,因为我想直接在url中传递参数,并将其保存在datasore&中.检索并打印它们.以下是我的代码.请谁能告诉我我哪里出问题了?
下面是一个Java类:

Hello friends, can someone tell me how to pass parameters in a url to call a servlet. I have not created any .jsp file ie no UI because I want to directly pass the parameters in url, save them in datasore & retrieve and print them. Following is my code. Pls can anyone tell me where I went wrong?
below is a java class:

package com.example.myproj;
//necessary packages are imported
@PersistenceCapable
public class User
{
    @PrimaryKey
    @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
    String user_id;
    @Persistent
    String name;
    @Persistent
    String email;
    @Persistent
    String isAdmin;
    public User(String name,String email,String isAdmin)
    {
        this.name=name;
        this.email=email;
        this.isAdmin=isAdmin;
    }
    public String getKey()
    {
        return user_id;
    }
    public String getName()
    {
        return name;
    }
    public String getMail()
    {
        return email;
    }
    public String checkAdmin()
    {
        return isAdmin;
    }
    public void setName(String name)
    {
        this.name=name;
    }
    public void setMail(String email)
    {
        this.email=email;
    }
    public void setAdmin(String isAdmin)
    {
        this.isAdmin=isAdmin;
    }

}



servlet文件:
包com.example.myproj;



The servlet file:
package com.example.myproj;

//necessary packages are imported
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
	public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
	{
		String name=req.getParameter("name");
		String email=req.getParameter("email");
		String isAdmin=req.getParameter("isAdmin");
		PersistenceManager pm=PMF.get().getPersistenceManager();
		User us=new User(name,email,isAdmin);
		try
		{
			pm.makePersistent(us);
			resp.setContentType("text/plain");
			resp.getWriter().println("Name="+name+"\nEmail-id="+email+"isAdmin="+isAdmin);
		}
		finally
		{
			pm.close();
		}
	}//doGet
	public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
	{
		doGet(req,resp);
	}
}

web.xml文件

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <servlet>
        <servlet-name>Quiztr</servlet-name>
        <servlet-class>com.example.myproj.QuiztrServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Quiztr</servlet-name>
        <url-pattern>/quiztr</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.example.myproj.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/UserServlet</url-pattern>
    </servlet-mapping>

</web-app>



还有一个类可以创建&检索用于链接到数据存储的pmfInstance.

follwing是我用来传递参数的网址.-http://localhost:8888/UserServlet?name = bhakti& email = cool_bhakti& isAdmin = yes

得到大声笑.错误:



there is one more class which creates & retrieves the pmfInstance used to link to the datastore.

follwing is the url I am using to pass para.- http://localhost:8888/UserServlet?name=bhakti&email=cool_bhakti&isAdmin=yes

getting the follo. error:

HTTP ERROR 405
Problem accessing /UserServlet. Reason:

    HTTP method GET is not supported by this URL



预先表示感谢.



Thanks in advance.

推荐答案

不支持"通常表明超类没有被正确覆盖.

请覆盖 doGet(args) doPost(args)-正如您已经做过的-并 NOT 调用覆盖的类(带有super.XY()).

您的web.xml文件还显示了2个servlet映射-请注意这一点!
the "not supported" often shows that the super-class is not overwritten correct.

please overwrite the doGet(args) and doPost(args) - as you''ve done already - and do NOT call any functions from the overwritten class (with super.XY() ).

Your web.xml file also shows 2 servlet mappings - be aware of that!


这篇关于在url中传递参数以调用servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆