1) How To Insert Data From XML File To SQL Server Database.: Servlet

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

1)how to insert data from xml file to Sql

server database.
Hi all,

I have data in the XML file. I need to insert it into table in the database using
servlet. so please reply me .

ThankYou

2)
ServletActionContext.getServletContext().get
ContextPath()
Hi,
im new to servlets,jsp and struts framework.my Doubt is that in our project they are
using the line ServletActionContext.getServletContext().getContextPath().What does
it mean?Can anyone clear my doubts?

3) Null pointer exception in HFSJ for context


listener

Hello forum,

I am following Head First Servlet JSP and wrote according to the book for context
listener but getting NullPointer exception in ListenerTester.java. Here is my code for
same:

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("test context attributes set by listener <br>");
out.println("<br>");
Dog dog = (Dog) getServletContext().getAttribute("dog"); // showing null here
out.println("Dog's breed is :" + dog.getBreed());
}

When I wrote this it was running well but after then it's showing null pointer
exception. If some one have any idea then please help.

4)

what is ment by servelet container ?


hi to all,
i am completely new to web technologies.
can you explain to me what is ment by servlet container and what is the
middleware tech that are userd to execute the web applications.

5)how to create a file on web server at


runtime

Hi

i want to create and save a file in inside my web application directory which is
runninig on server. How can i do that? How do I get the absolute path of my project
directory? My web server is apache tomcat.

Please Help

Can I change the response mime type


6)
midway?
Hi

I wish to send a jpeg file to the browser followed by some text. How can i achieve
that? Do i have to change the content type after writing the jpeg file in output
stream to plain text?

Thanks
utsav.
7) Send
a "Please Wait" Page, then send a
"Transaction Completed" Page

I am writing a servlet to perform a financial transaction. I can re-write it as several


related servlets if necessary. Rather than bog down on details, I will explain what I
want to accomplish, and the experienced among you will be able to advise me on
how to approach this.

A doPost calls my servlet, sending it about 20 parameters. The servlet checks to


make certain that the parameters are valid. If they are not, it sends a response,
advising the user what is wrong, and allows for a back page to fix the errant items.
All this works. My problem arise once the servlet is satisfied with the parameters,
and gets down to executing the business logic.

What I want to do is display a "Please wait, we are processing your request" web
page while my servlet is chugging away. After the servlet has finished, I want to
display a page "Transaction is completed, print this for your records". I want the
Transaction Completed page to replace the Please Wait page, but I could live with
Transaction Completed appearing in a separate tab or window.

I have experimented with various forms of flush, response.setHeader(Refresh),


RequestDispatcher, and redirect, but nothing quite works the way that I want it. I
would prefer to avoid Javascript, but I would do it if I must.

I don't think that I need detailed code at this point. I think that I need general
guidance. This is a common web transaction. I must be missing something in the
bigger picture.

Thanks to all who respond,


Josef

8)

Problems with sessions


I think that my problem is wierd and even though I searched the forum I couldnt find
the solution. So here is my doubt regarding sessions. I am using servlets and JSP's
for testing sessions. I am using Tomcat Server(IDK if different servers handle
sessions in different ways). Every time I send a request from a jsp page which is the
first page for my application.. it hits a servlet. So as this is my first page there
should not be any session existing. So when I do request.getsession(); I get a
session ID and when ever I try to check the properties of that session variable while
debugging it shows that the session is not new. I invalidate all the sessions before
calling the first page. But things are the same. Ultimately what I want over here is,
when a user clicks my first page and enters his name it should store in sessions.
And every time I display a page it should show his name in all other pages. I added
the username that I got from the first page into the session variable "only if the
session is new".. But unfortunately I could not create a new session.

view plaincopy to clipboardprint?


1. <form action="DemoServlet" method="get">
2. Please enter your name
3. <input type="text" name="username"></input><br>
4. Select one:
5. <select name="option1">
6. <option value="1">First page</option>
7. <option value="2">Second page</option>
8. <option value="3">Third page</option>
9. </select>
10. <input type="submit" value="Submit this page"/>
11. </form>
<form action="DemoServlet" method="get">
Please enter your name
<input type="text" name="username"></input><br>
Select one:
<select name="option1">
<option value="1">First page</op
<option value="2">Second page<
<option value="3">Third page</op
</select>
<input type="submit" value="Submit this pag

view plaincopy to clipboardprint?


1. protected void doPost(HttpServletRequest request,
2. HttpServletResponse response) throws ServletException, IOException
{
3. HttpSession s = request.getSession(true);
4. String username = (String) request.getParameter("username");
5. String option1 = (String) request.getParameter("option1");
6. ArrayList<String> al = new ArrayList<String>();
7. System.out.println(s.getId()+username);
8. if (s.isNew()) {
9. System.out.println("welcome new user");
10. s.setAttribute("username", username);
11.
12. }
13. else {
14. System.out.println("welcome existing user"
15. + s.getAttribute("username"));
16. }
17.
18. if(option1.equals("1"))
19. {
20. al.add("option1value1");
21. al.add("option1value2");
22. al.add("option1value3");
23. }
24. else if(option1.equals("2"))
25. {
26. al.add("option2value1");
27. al.add("option2value2");
28. al.add("option2value3");
29. }
30. else if(option1.equals("3"))
31. {
32. al.add("option3value1");
33. al.add("option3value2");
34. al.add("option3value3");
35. }
36. request.setAttribute("show1", al);
37. request.setAttribute("username", s.getAttribute("username"));
38. request.getRequestDispatcher("/show1.jsp").forward(request, response);

39. }
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
HttpSession s = request.getSession(true);
String username = (String) request.getPara
String option1 = (String) request.getParame
ArrayList<String> al = new ArrayList<Strin
System.out.println(s.getId()+username);
if (s.isNew ()) {
System.out.println("w elcome new
s.setAttribute("username", usern

I did session.invalidate() few hundred times before clicking my first page. But still
my output is
59908F11DF53149C9DAD6A244CAC2EC7hello
welcome existing usernull

I tried all the ways but useless. At times I feel I want to maintain an arraylist for

checking the sessions . Is there any method which creates a new session???
Really I feel frustrated. I have been trying to sort this out since more than a week .
Can anyone help me???

(9) How come I can pull a session attribute in


a JSP page but not in a Servlet?
I have a web application that begins with a login page. During the verification of
that login a session is created and the ID used to login is saved as an attribute. That
ID is then suppose to be displayed in the pages that user navigates to. So far I can
only get it to display in a JSP page but everything I try in a Servlet page does not
work.

Here is the logic I used in my login verification page:

view plaincopy to clipboardprint?


1. UserBean2 userBean2 = (UserBean2) session.getAttribute("userBean2");
2. String un = request.getParameter("id");
3. userBean2 = new UserBean2(un);
4. session.setAttribute("userBean2", userBean2);
UserBean2 userBean2 = (UserBean2) session.getAttribute("us
String un = request.getParameter("id");
userBean2 = new UserBean2(un);
session.setAttribute("userBean2", userBean2);

Here is the Bean I created UserBean2:

view plaincopy to clipboardprint?


1. public class UserBean2 {
2. private String id;
3.
4. public UserBean2(String id) {
5. setUsername(id);
6. }
7.
8. public String getUsername() {
9. return(id);
10. }
11.
12. public void setUsername(String id) {
13. if (!isMissing(id)) {
14. this.id = id;
15. }
16. }
17.
18. private boolean isMissing(String value) {
19. return(value == null) || (value.trim().equals("")));
20. }
21. }
public class UserBean2 {
private String id;

public UserBean2(String id) {


setUsername(id);
}

public String getUsername() {


return(id);
}

The part that works is when I try to call the ID in a jsp page. I do that with this:

view plaincopy to clipboardprint?


1. ${userBean2.username}
${userBean2.username}

But to call it in a Servlet page I've tried everything under the sun and nothing has
worked correctly.

I even tried a getAttributeNames thinking that would give me the info I needed but
all I received was this: (bokay is the ID that was used to login to this particular
session)

bokay: bokay
userBean2: HWpackage.UserBean2@257ccb2f

Any ideas as to what I'm suppose to use in the JSP page to display the ID, which in
this case happens to be "bokay"?

Thanks everyone!

10) Servlet filter to restrict GET request


Hello ,

I am facing the below issue in my j2ee application.

The application code is written to support POST requests.


But if I try to access my application through the URL with the values ( since I am the
coder, I know the hidden paramters)
then I can access the website bypassing the login page.

I have a servlet filter - BuT I am not sure is this a right approach to block the
request at this level. Can anyone guide me?

PP

You might also like