Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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;
    }

}


The servlet file:
package com.example.myproj;
C#
//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);
	}
}
the web.xml file
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>


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:
CSS
HTTP ERROR 405
Problem accessing /UserServlet. Reason:

    HTTP method GET is not supported by this URL


Thanks in advance.
Posted
Updated 24-Sep-11 10:24am
v2

1 solution

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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900