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;
@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;
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();
}
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
doGet(req,resp);
}
}
the web.xml file
="1.0"="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:
HTTP ERROR 405
Problem accessing /UserServlet. Reason:
HTTP method GET is not supported by this URL
Thanks in advance.