Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to run this servlet file in netBeans IDE 6.9.1 ?

 
Trupti brane
Greenhorn
Posts: 20
C++ Java Windows Vista
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.ServletConfig;
//import java.util.*;
public class CookieServlet extends HttpServlet {
public void init(ServletConfig config)throws ServletException{
super.init(config);
}

private String getCurrentUser(String value)
{
String userName=new String(" ");

if(value.equals("564xx892"))
{
userName=new String("Trupti");
}
return userName;

}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

Cookie[] cookielist=request.getCookies();
String user=null;
String responseString=null;

if(cookielist!=null)
{
for(int x=0;x<cookielist.length;x++)
{
String name=cookielist[x].getName();

if(name.equals("session_id"))
{
user=getCurrentUser(cookielist[x].getValue());
break;
}
}
}

if(user==null)
{
response.addCookie(new Cookie("session_id","564xx892"));
responseString=new String("welcome to our site,"+"we have created session for you");
}
else
{
responseString=new String ("hello"+user);
}
response.setContentType("html/text");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head><title>CookieServlet</title></head>");
out.println("<body>");
out.println(responseString);
out.println("</body>");
out.println("</html>");
out.close();
}



public String getServletInfo()
{
return "CookieServlet information";
}// </editor-fold>

}


i wanted to run this file in IDE netBeans.i know how to run html file which will show the output in browser immediately after running.but this is .java file which i am unable to see in browser.soon after i run this file my browser download this file and ask me to select the program to open the file.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this link will help you:
http://www.java-tips.org/java-tutorials/tutorials/introduction-to-java-servlets-with-netbeans.html

Next time you post, you coud wrap your code in the code tags. It will be easier to read. [=
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic